如何使用ios中的大文件上传? [英] How to work with large file uploads in ios?

查看:134
本文介绍了如何使用ios中的大文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用需要从用户手机上传视频文件,然后在服务器上处理。
问题是文件的大小可以达到200 MB以上,用户不会保持应用程序打开以等待文件上传。由于苹果不允许应用程序在后台运行超过有限的时间。如何确保上传文件。我正在使用afnetworking来设置ios 7库给出的上传任务。



如果有人能指出我正确的方向或有任何解决方案,那将是非常重要的赞赏。我已经把这个问题拖了很长时间。谢谢。

  NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];


[manager setTaskDidSendBodyDataBlock:^(NSURLSession * session,NSURLSessionTask * task,int64_t bytesSent,int64_t totalBytesSent,int64_t totalBytesExpectedToSend){
CGFloat progress =((CGFloat)totalBytesSent /( CGFloat的)sensize);

NSLog(@上传文件%lld - >%lld,totalBytesSent,totalBytesExpectedToSend);
[self.delegate showsProgress:progress forIndex:ind];
}];



dataTask = [manager uploadTaskWithStreamedRequest:请求进度:nil completionHandler:^(NSURLResponse *响应,id responseObject,NSError *错误){
if(error) {
NSLog(@错误:%@,错误);
}其他{

}

}];

我的请求是正常的多部分表单请求。

  NSURLSessionConfiguration:backgroundSessionConfiguration:

而不是

  NSURLSessionConfiguration: defaultSessionConfiguration 

来自 NSURLSessionConfiguration:backgroundSessionConfiguration:文档


后台会话中的上传和下载任务由外部守护程序而不是应用程序本身执行。因此,即使应用程序被暂停,退出或崩溃,转移也会在后台继续。


所以在你的情况下,更改:

  NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 

to:

  NSString * appID = [[NSBundle mainBundle] bundleIdentifier]; 
NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:appID];

实施应用程序:handleEventsForBackgroundURLSession:completionHandler: on您的应用代表将允许您的应用在上传完成后被唤醒(即在后台模式下未暂停或未终止)(无论是否已成功完成)。



不要与背景提取混淆。你不需要它。背景提取简单地唤醒你应用程序定期给你的应用程序定期获取少量内容的机会。但是,它可能会定期重新启动失败的后台模式上传。


My app requires upload of video files from users phone which will then be processed on server. THe problem is the size of the file can go to 200 MB plus and the user won't keep the application open to wait for the file to upload. Since apple doesn't allow apps to run in background for more than a limited time. How can I ensure that my files are uploaded. I am using afnetworking to set up an upload task as given by ios 7 library.

Please if anyone can point me in the right direction or have any solution it would be greatly appreciated. I have banged my head on this for too long. Thanks.

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];


    [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session,NSURLSessionTask *task ,int64_t bytesSent, int64_t totalBytesSent,int64_t totalBytesExpectedToSend){
        CGFloat progress = ((CGFloat)totalBytesSent / (CGFloat)sensize);

       NSLog(@"Uploading files %lld  -- > %lld",totalBytesSent,totalBytesExpectedToSend);
        [self.delegate showingProgress:progress forIndex:ind];
    }];



    dataTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {

        }

       }];

My request is a normal multipart form request.

解决方案

Use:

NSURLSessionConfiguration:backgroundSessionConfiguration:

instead of

NSURLSessionConfiguration:defaultSessionConfiguration

From the NSURLSessionConfiguration:backgroundSessionConfiguration: documentation:

Upload and download tasks in background sessions are performed by an external daemon instead of by the app itself. As a result, the transfers continue in the background even if the app is suspended, exits, or crashes.

So in your case, change:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

to:

NSString *appID = [[NSBundle mainBundle] bundleIdentifier];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:appID];

Implementing application:handleEventsForBackgroundURLSession:completionHandler: on your app delegate will allow your app to be woken up (ie. un-suspended or un-terminated in background mode) when an upload has completed (whether it has completed successfully or not).

Don't get confused with Background Fetching. You don't need it. Background Fetching simply wakes you app up to periodically give your app the chance to fetch small amounts of content regularly. It may however, be useful for restarting failed "background-mode" uploads periodically.

这篇关于如何使用ios中的大文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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