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

ios – 如何从iPhone上传视频到服务器?,第1张

概述-(IBAction)uploadToServer :(id)sender{ NSString *str1=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"intro.mp4"]; NSLog(@"str1=%@",str1); NSString *escapedUrlStri
-(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];
总结

以上是内存溢出为你收集整理的ios – 如何从iPhone上传视频到服务器?全部内容,希望文章能够帮你解决ios – 如何从iPhone上传视频到服务器?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109549.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存