dongyingdao8867 2011-12-05 09:56
浏览 50
已采纳

点击按钮上传视频

My app requires to upload video on click of a button. However, I can't do it with my code. My code for this is as follows:

- (IBAction)uploadVideo {    

/* setting up the URL to post to */

NSString *urlString = @"http://172.19.128.170/UploadFile.php";

/* setting up the request object */

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

/* setting up the request body*/

NSString *path = [[NSString alloc]init];
path = @"Users/msat/Library/ApplicationSupport/iPhoneSimulator/4.3.2/Media/DCIM/100APPLE/";
NSString *boundary = [NSString stringWithString:@"*****"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@", boundary, @"
"] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"uploadeFfile\"; filename=\"Users/msat/Library/ApplicationSupport/iPhoneSimulator/4.3.2/Media/DCIM/100APPLE/IMG_0001.png""
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"--
"] dataUsingEncoding:NSUTF8StringEncoding]];

// setting up the buffer size 

int buffer[BUFFER_SIZE];
int bytesRead;

while ((bytesRead = read([fileHandle fileDescriptor], buffer, BUFFER_SIZE) > 0)) {

    [body appendBytes:buffer length:(NSUInteger)bytesRead];
}

[body appendData:[[NSString stringWithFormat:@"buffer"]dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithString:@"Content-Type:"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@", boundary, @"--
"] dataUsingEncoding:NSUTF8StringEncoding]];

/* setting the body of the post to the reqeust */

[request setHTTPBody:body];

/* setting up the connection to the web*/
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSLog(@"...this is returned %@", returnData);

NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"...this is uploaded %@", returnString);

}

The PHP I am using is as follows:

<?php
// Where the file is going to be placed 
$target_path = "./Videos/Unapproved/";
//$description = $_FILES['description']['name'];
//echo "Entered the PHP file!!" . basename( $_FILES['description']['name']);

/* Add the original filename to our target path.  
Result is "/tmp/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
echo "before checking the file!". basename( $_FILES['uploadedfile']['name']);
//echo $_FILES['uploadedfile']['tmp_name'];

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). 
" has been uploaded";
//$conn = mysql_connect("localhost", "root", "demo123") or die(mysql_error()); 
//mysql_select_db("Upload") or die(mysql_error()); 
//mysql_query("INSERT INTO User_Upload_Table(video_Name, description, video_Path, duration, status)  VALUES('', '', '', '', '')");
//mysql_close($conn);
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['uploadedfile']['name']);
echo "target_path: " . $target_path;
}
?>

Can somebody tell me where am I making mistake?

What else do I need to do if I want to stream a video and upload it on the server?

  • 写回答

1条回答 默认 最新

  • dongzhuner6981 2011-12-05 10:14
    关注

    Here is the code I have used to upload a file to a server :

    http://pastebin.com/bk7M0rr5

    As far as the php goes, the code looks fine. And you see/use the code above or compare it with yours to find the issue

    UPDATE

    //declaring vars
    $username = $_GET['user'];
    $status = $_GET['tweet'];
    $uploaddir = '/var/www/sounds/';      //Uploading to sounds dir
    $file = basename($_FILES['userfile']['name']);
    $uploadFile = $file;
    $uName = "i-".$username."_".time(); 
    $newName = $uName; // . $uploadFile;
    
    
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    
    } else {
        echo "error:100";
    }
    
    $caf = $uploaddir . $newName.'.caf';
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $caf)) {
    //here I added a few lines to convert the audio file to mp3 etc and move it around
    
    //    echo $rvalue;
    //this is where you can return something
    }
    

    NOTE that I have changed the code a bit for security.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题