我正在尝试将大文件从 iOS 上传到服务器
我的代码启动 webservce 并在上传 ~180000 字节后挂起、等待并最终超时。
正如代码当前所在的那样。(我已经改变了很多试图排除故障)
AFHTTPClient *uploadClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:k_WebService_Root]];
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"secret" , @"Token",
nil];
NSDictionary *requestObject = [NSDictionary dictionaryWithObject:parameters forKey:@"request"];
NSString *endpoint = [self makeEndpointWithService:k_ServiceEndpoint_Message andAction:k_WebService_Message_Upload];
NSMutableURLRequest *request = [uploadClient multipartFormRequestWithMethod:@"POST" path:endpoint parameters:requestObject constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *data = [NSData dataWithContentsOfFile: videoFile.path];
[formData appendPartWithFileData:data name:@"file" fileName:@"message.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(@"Success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Fail\n%@", error);
}];
[operation start];
输出总是这样
2012-07-30 12:42:20.747 BMP[3290:3503] 已发送 32768 个字节,共 2316966 个字节 2012-07-30 12:42:20.754 BMP[3290:3503] 已发送 65536 个字节,共 2316966 个字节 2012-07-30 12:42:20.760 BMP[3290:3503] 已发送 98304 个字节,共 2316966 个字节 2012-07-30 12:42:20.765 BMP[3290:3503] 已发送 131072 个字节,共 2316966 个字节 2012-07-30 12:42:21.113 BMP[3290:3503] 已发送 133452 个字节,共 2316966 个字节 2012-07-30 12:42:21.351 BMP[3290:3503] 发送 136148 个字节,共 2316966 个字节 2012-07-30 12:42:21.610 BMP[3290:3503] 发送 138844 个字节,共 2316966 个字节 2012-07-30 12:42:21.940 BMP[3290:3503] 已发送 141540 个字节,共 2316966 个字节 2012-07-30 12:42:22.030 BMP[3290:3503] 发送 144236 个字节,共 2316966 个字节 2012-07-30 12:42:22.120 BMP[3290:3503] 发送 146932 个字节,共 2316966 个字节 2012-07-30 12:42:22.142 BMP[3290:3503] 已发送 149628 个字节,共 2316966 个字节 2012-07-30 12:42:22.170 BMP[3290:3503] 发送 152324 个字节,共 2316966 个字节 2012-07-30 12:42:22.209 BMP[3290:3503] 发送 155020 个 2316966 字节 2012-07-30 12:42:22.240 BMP[3290:3503] 已发送 157716 个字节,共 2316966 个字节 2012-07-30 12:42:22.488 BMP[3290:3503] 已发送 160412 个字节,共 2316966 个字节 2012-07-30 12:42:22.668 BMP[3290:3503] 已发送 163108 个字节,共 2316966 个字节 2012-07-30 12:42:22.900 BMP[3290:3503] 已发送 163840 个字节,共 2316966 个字节 2012-07-30 12:42:23.003 BMP[3290:3503] 已发送 167152 个字节,共 2316966 个字节 2012-07-30 12:42:23.141 BMP[3290:3503] 发送 171196 个字节,共 2316966 个字节 2012-07-30 12:42:23.181 BMP[3290:3503] 已发送 173892 个字节,共 2316966 个字节 2012-07-30 12:42:23.211 BMP[3290:3503] 发送 176588 个字节,共 2316966 个字节 2012-07-30 12:42:23.251 BMP[3290:3503] 已发送 179284 个字节,共 2316966 个字节 2012-07-30 12:42:23.303 BMP[3290:3503] 发送 181980 个字节,共 2316966 个字节 2012-07-30 12:42:23.353 BMP[3290:3503] 已发送 184676 个字节,共 2316966 个字节 2012-07-30 12:42:23.416 BMP[3290:3503] 已发送 187372 个字节,共 2316966 个字节
然后几分钟后,我从 AFNetworking 获得超时
有任何想法吗?
更新 ::: 显然它只发生在通过 3G 上传时