2014-02-21 67 views
1

我試圖上傳一個視頻到服務器。我有兩種選擇來上傳視頻。首先是,把視頻上傳到服務器。第二個是,從照片庫中選擇視頻並將其上傳到服務器。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]; 
} 

提供任何想法來解決這個問題。

回答

0

你能描述一下你的服務器端嗎?到目前爲止,我明白這可能是一個完整的URL問題。也許你的json片段?也許查看您正在寫入的文件夾的權限。如果你使用ubuntu和lamp作爲堆棧(noob我知道),確保它是r + w,但不是exec。

0
//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']);