HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 sethttpR_246_11845@ethod:@"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);
}
@H_262_4@

解决方法

使用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 @R_513_10586@lBytesWritten,long long @R_513_10586@lBytesExpectedToWritE)
 {

     NSLog(@"Sent %lld of %lld bytes",@R_513_10586@lBytesWritten,@R_513_10586@lBytesExpectedToWritE);

 }];

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


[operation start];
@H_262_4@ @H_262_4@

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。