ios – 如何从iPhone上传视频到服务器?

-(IBAction)uploadToServer :(id)sender
{
    Nsstring *str1=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"intro.mp4"];
    NSLog(@"str1=%@",str1);

    Nsstring *escapedUrlString = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"escapedUrlString=%@",escapedUrlString);

    NSURL *videoURL = [NSURL URLWithString:escapedUrlString];
    NSLog(@"videoURL=%@",videoURL);

    NSData *newdata = [NSData dataWithContentsOfFile:escapedUrlString];
    webdata=[NSData dataWithData:newdata];
    NSLog(@"webData = %@",webdata);
   [self post:webdata];
    }

- (void)post:(NSData *)fileData
{

    NSData *videoData = fileData;
    Nsstring *urlString = @"http://rompio.com/web_service/web.PHP?method=upload_video&user_id=4";

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

    Nsstring *boundary = @"---------------------------14737809831466499882746641449";
    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:[@"Content-disposition: form-data; name=\"userfile\"; filename=\".mp4\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:videoData]];
    [body appendData:[[Nsstring stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    Nsstring *returnString = [[Nsstring alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"returnString=== %@",returnString);
}

解决方法

使用AFNetworking库很容易,您也可以使用它来跟踪视频上传的进度.您可以从 here下载AFNetworking库.

有关配置AFnetworking的信息,请参阅Link.

代码将用于在服务器上发送视频

Nsstring *videoURL = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mov"];
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath: videoURL]];

AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.example.com"]];

NSMutableuRLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/videoupload.PHP" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
{
    [formData appendPartWithFileData:videoData name:@"file" fileName:@"myVideo.mov" mimeType:@"video/quicktime"];
}];



AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request];

[operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite)
 {

     NSLog(@"Sent %lld of %lld bytes",totalBytesWritten,totalBytesExpectedToWrite);

 }];

[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responSEObject) {NSLog(@"Video Uploaded Successfully");}
                                  failure:^(AFHTTPRequestOperation *operation,NSError *error) {NSLog(@"Error : %@",operation.responseString);}];


[operation start];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...