如何从 iPhone 上传视频到服务器? [英] How to Upload video to server from iPhone?

查看:24
本文介绍了如何从 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:@"
--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name="userfile"; filename=".mp4"
" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:videoData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
", 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 库很容易,您还可以使用它来跟踪视频上传的进度.您可以从此处下载 AFNetworking 库.

It's easy to do with the AFNetworking library and you can also use it to track the progress of the video upload. You can download AFNetworking library from here.

有关配置 AFnetworking,请参阅此链接.

And for configuring AFnetworking please refer this Link.

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

And this code will used to send the video on server

 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];

这篇关于如何从 iPhone 上传视频到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆