我正在使用AFNetworking
多部分 POST 将文件上传到 Web 服务。使用 WiFi 时一切正常,但通过 3g 上传时,对于大于 2 兆字节的文件,上传失败。
我尝试了两种方法,都在发送的确切字节数处失败:1998848。如果文件小于该文件,则上传成功。
Amazon S3 SDK 也有同样的问题,他们建议限制上传,但我找不到使用AFNetworking
or的方法NSUrlConnection
。
我还记得 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];
谢谢,
和你