2012-06-28 41 views
0

我使用AFNetworking使用多部分POST将文件上传到Web服务。使用WiFi时一切正常,但当通过3G上传时,对于大于2兆字节的文件,上传失败。当使用3G上传时,AFNetworking“无法分配内存”

我已经尝试了两种方法,都发送的字节的确切数量失败:1998848.如果文件或文件小于那么上传成功。

亚马逊S3 SDK有同样的问题,他们建议调节上传,但我找不到与AFNetworkingNSUrlConnection这样做的方法。

我还记得iOS在流式传输时每5分钟有5个兆字节的限制。我不确定这是否适用,但以我的3G速度,我在一分钟内上传了大约2兆字节。也许这可能是它?再次,无法找到任何相关的信息。

任何想法?这里是我的代码:

多部分上传:

NSMutableURLRequest *request = [[DIOSSession sharedSession] multipartFormRequestWithMethod:@"POST" path:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, @"demotix_services_story/upload"] parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) { 
    for (NSString * fkey in files) { 
     NSDictionary *file = [files objectForKey:fkey]; 
     NSURL *fileUrl = [NSURL fileURLWithPath:[file valueForKey:@"fileUrl"]]; 
     NSString *fileName = [file valueForKey:@"name"]; 

     [formData appendPartWithFileURL:fileUrl name:fileName error:&error]; 
    } 

}]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
    [delegate updateUploadProgress:totalBytesWritten from:totalBytesExpectedToWrite]; 
}]; 
[operation setCompletionBlockWithSuccess:success failure:failure]; 
[operation start]; 

将所有文件合并成一个,并尝试流呢:

NSURLRequest *nrequest = [[DIOSSession sharedSession] requestWithMethod:@"POST" path:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, @"demotix_services_story/upload"] parameters:params]; 

AFHTTPRequestOperation *noperation = [[AFHTTPRequestOperation alloc] initWithRequest:nrequest]; 
noperation.inputStream = [NSInputStream inputStreamWithFileAtPath:storePath]; 
noperation.outputStream = [NSOutputStream outputStreamToMemory]; 
[noperation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
    [delegate updateUploadProgress:totalBytesWritten from:totalBytesExpectedToWrite]; 
}]; 
[noperation setCompletionBlockWithSuccess:success failure:failure]; 
[noperation start]; 

感谢,

安都

+0

“我还记得当流式传输时,iOS每5分钟限制5MB。” - 这是由审查小组(HIGuidelines)施加的人为限制,与操作系统本身无关。此外,此限制与音频/视频流式传输有关,不会上传或下载任意内容。 – Till

+0

感谢您的解释。 –

+0

您是否找到解决方案?我遇到了同样的问题@AlexandruBadiu –

回答

1

ASIHTTPRequest支持带宽限制。

如果你不愿意切换,你可以分析它的代码,看看它是如何关闭以及是否可以扩展AFNetworking来做同样的事情。看看这些方法:

scheduleReadStream 
unscheduleReadStream 

setShouldThrottleBandwidthForWWAN 
+0

我用ASIHTTPRequest做了一个快速测试。如果不加限制,它会发生完全相同的错误,同时限制带宽工作正常。 –