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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里