大佬教程收集整理的这篇文章主要介绍了ios – 我需要在一个流媒体请求中使用AFNetworking将大量文件(最多100个)上传到服务器,这可以为我提供上传进度,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将大量文件(~50MB)上传到服务器,并且请求必须要流式传输. (否则app会因内存压力而崩溃)
我已尝试过各种方法在AFNetworking 2.0中做到这一点而没有任何成功.
这就是我目前正在做的事情:
NSMutableURLrequest *request = [[AFhttprequestserializer serializer] multipartFormrequestWithMethod:@"POST" URLString:baseserviceUrl parameters:nil construcTingBodyWithBlock:^(id<AFMultipartFormData> formData) { int imageIndex = 0; for (NSURL *tempFileUrl in tempFilesUrlList) { NSString* filename = [NSString StringWithFormat:@"image%d",imageIndex]; NSError *appendError = nil; [formData appendPartWithFileURL:tempFileUrl name:filename error:&appendError]; imageIndex++; } } error:&error]; AFhttprequestOperation *requestOperation = nil; requestOperation = [manager httprequestOperationWithrequest:request success:^(AFhttprequestOperation *operation,id responSEObject) { DLog(@"Uploading files completed: %@\n %@",operation,responSEObject); } failure:^(AFhttprequestOperation *operation,NSError *error) { DLog(@"Error: %@",error); }]; [requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten,long long @R_183_10586@lBytesWritten,long long @R_183_10586@lBytesExpectedToWritE) { double percentDone = (doublE)@R_183_10586@lBytesWritten / (doublE)@R_183_10586@lBytesExpectedToWrite; DLog(@"progress updated(percentDonE) : %f",percentDonE); }]; [requestOperation start];
这工作正常,但它不流.如果请求太大,它准备内存中的请求可能会崩溃.我曾尝试使用原生套接字和流,但Apple表示他们可能因此而拒绝该应用.
我尝试过的另一种方法是:
AFhttpSessionManager* manager = [AFhttpSessionManager manager]; NSString *appID = [[NSBundle mainBundle] bundlEIDentifier]; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration BACkgroundSessionConfiguration:appID]; NSURL* serviceUrl = [NSURL URLWithString:baseserviceUrl]; manager = [manager initWithBaseURL:serviceUrl sessionConfiguration:configuration]; NSURLSessionDataTask *uploadFilesTask = [manager POST:baseserviceUrl parameters:nil construcTingBodyWithBlock:^(id<AFMultipartFormData> formData) { int imageIndex = 0; for (NSURL *tempFileUrl in tempFilesUrlList) { NSString* filename = [NSString StringWithFormat:@"image%d",imageIndex]; NSError *appendError = nil; [formData appendPartWithFileURL:tempFileUrl name:filename error:&appendError]; imageIndex++; } } success:^(NSURLSessionDataTask *task,id responSEObject) { DLog(@"Files uploaded successfully: %@\n %@",task,responSEObject); } failure:^(NSURLSessionDataTask *task,error); }]; [progressBar setProgressWithUploadProgressOfTask:uploadFilesTask animated:true];
(必须是流媒体,并且能够在后台继续)
我建议使用AFNetworking等网络库.它将使您免于执行之前已解决的单调任务.
为了在后台真正发送请求,您需要将整个请求写入文件,然后使用NSURLSession将其传输到服务器. (这是接受的答案 – 下面的链接)因此,您需要将请求写入磁盘而不将整个文件读入内存.
How to upload task in background using afnetworking
NSMutableURLrequest *request = [[AFhttprequestserializer serializer] multipartFormrequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil construcTingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" filename:@"filename.jpg" mimeType:@"image/jpeg" error:nil]; } error:nil]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; NSProgress *progress = nil; NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedrequest:request progress:&progress completionHandler:^(NSURLResponse *response,id responSEObject,NSError *error) { if (error) { NSLog(@"Error: %@",error); } else { NSLog(@"%@ %@",response,responSEObject); } enter code here }]; [uploadTask resume];
http://cocoadocs.org/docsets/AFNetworking/2.5.0/
注意:@L_772_33@简单的解决方案是请求您的应用在后台运行的时间.当你的app delegate上调用applicationDidEnterBACkground:时,你可以调用[[UIApplication sharedApplication] beginBACkgroundTaskWithExpirationHandler:]来请求一些时间在后台运行. ([[UIApplication sharedApplication] endBACkgroundTask:];说你已经完成了.)你问的有什么缺点?您将获得有限的时间. “如果你没有在时间到期之前调用endBACkgroundTask:对于每个任务,系统会杀死应用程序.如果你在handler参数中提供@L_772_33@块对象,系统会在时间到期之前调用你的处理程序,让你有机会结束任务. “
进展
您可以使用子/父关系跟踪多个NSProgress对象的进度.您可以创建@L_772_33@NSProgress * overallProgress对象,然后调用[overallProgress becomeCurrentWithPendingUnitCount:< unit size>];在创建新请求和[overallProgress resignCurrent]之前;然后.您的overallProgress对象将反映所有孩子的进度.
以上是大佬教程为你收集整理的ios – 我需要在一个流媒体请求中使用AFNetworking将大量文件(最多100个)上传到服务器,这可以为我提供上传进度全部内容,希望文章能够帮你解决ios – 我需要在一个流媒体请求中使用AFNetworking将大量文件(最多100个)上传到服务器,这可以为我提供上传进度所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。