iOS上的视频上传到服务器
问题描述:
我试图上传一个视频到服务器。我有两种选择来上传视频。首先是,把视频上传到服务器。第二个是,从照片库中选择视频并将其上传到服务器。iOS上的视频上传到服务器
第1步: 在这种情况下,将视频并上传到服务器运行良好。
我从服务器得到的回应是“返回字符串:{”jsonstatus“:”ok“,”message“:”感谢您的发帖。“}”。
现在我尝试从服务器获取此视频。在media_file中显示“x.x.x.x/video/96b8954fbed797500da708fd7bad2261video.mov” - 我成功播放了该视频。
第2步: 但是,当我从照片库中选择视频,它成功地选择了视频。现在我发布这个视频到服务器。
我从服务器得到的回应是“返回字符串:{”jsonstatus“:”ok“,”message“:”感谢您的发帖。“}”。
现在我尝试从服务器获取此视频。在media_file提示“无效文件”
正在使用选择视频,
- (IBAction)act_UploadVideoBtn:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentViewController:imagePicker animated:YES completion:NULL];
}
和委托方法,
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// movieURL = [info valueForKey:UIImagePickerControllerMediaURL];
//
// [picker dismissViewControllerAnimated:YES completion:NULL];
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
// NSLog(@"%@",moviePath);
movieURL=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
}
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
提供任何想法来解决这个问题。
答
你能描述一下你的服务器端吗?到目前为止,我明白这可能是一个完整的URL问题。也许你的json片段?也许查看您正在写入的文件夹的权限。如果你使用ubuntu和lamp作为堆栈(noob我知道),确保它是r + w,但不是exec。
答
//push video path to NSData
NSData *webData = [NSData dataWithContentsOfURL:movieURL];
NSURL *url = [NSURL URLWithString:@"http://www.example.com/upload_media.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"+++++ABck";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;
boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"YOURFILENAME\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: video/quicktime\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];//video/quicktime for .mov format
[body appendData:[NSData dataWithData:webData]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"%@",response);
NSLog(@"%@",error);
/*Server code*/
move_uploaded_file($_FILES["file"]["tmp_name"],$upload_dir['basedir'].'YOUR_DIR']);