Uploading large files on iOS with AFNetworking - e

2019-05-28 20:53发布

I'm facing with the next problem. In my project, I'm using AFNetworking for all network operations. One of them is uploading video the the server. Then I'm trying to upload large video (about 100 Mb), I'm getting request timeout error.

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x15641b30
{NSErrorFailingURLStringKey=http://server.name/path, NSErrorFailingURLKey=http://server.name/path, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x16f7a000 "The request timed out."}

Now I'm using AFNetworking v1.3.3, and I can't use v2.0 because iOS5 support is a requirement.

When upload is just starting, upload progress looks fine (I see it through UploadProgressBlock). But after few megabytes, upload starts slowing, and later it stops. SpeedTest gives me 5Mbps for upload, and 5Mbps for download.

Upload video through web browser is working fine, so I don't think that is server problem.

Here is my code:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL: 
            [NSURL URLWithString:@"http://server.name/"]];
NSString *appid = [[self class] sharedProvider].ApplicationId;

ALAssetRepresentation *representaion =
             [videoData.videoAsset defaultRepresentation];
NSURL *url =
 [BRDataProvider getVideoAssetURLForTempFileWithAsset:
                                 videoData.videoAsset];

AFHTTPRequestOperation *operation;

if (url) {
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" 
    path:@"some/path" parameters:nil

constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

  NSData *hdnADCID = [appid dataUsingEncoding:NSUTF8StringEncoding];
     [formData appendPartWithFormData:hdnADCID name:@"hdnADCID"];
    NSData *txtTitle =
         [videoData.title dataUsingEncoding:NSUTF8StringEncoding];

 [formData appendPartWithFormData:txtTitle name:@"txtTitle"];

      NSData *txtDescription =
             [videoData.description dataUsingEncoding:NSUTF8StringEncoding];
    [formData appendPartWithFormData:txtDescription name:@"txtDescription"];

  NSData *txtKeywords =
       [videoData.tags dataUsingEncoding:NSUTF8StringEncoding];
    [formData appendPartWithFormData:txtKeywords name:@"txtKeywords"];
       [formData
     appendPartWithFileURL:url name:representaion.filename error:nil];
 }];
[request setTimeoutInterval:600];
operation = [fliqzClient HTTPRequestOperationWithRequest:request
   success:^(AFHTTPRequestOperation *operation, id responseObject) {

  [[NSFileManager defaultManager] removeItemAtURL:url error:nil];

   NSString *assetID = [operation.responseString
 stringByReplacingOccurrencesOfString:@"&\r\n" withString:@""];
       assetID = [assetID stringByReplacingOccurrencesOfString:@"id=
                           " withString:@""];
       videoData.assetId = assetID;
       [BRDataProvider registerVideoWithInfo:videoData completion:^(id result,
              NSError *error) {
          block(result,error);
           }];
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

               NSLog(@"error - %@", error);

                block(nil,error);
    [[NSFileManager defaultManager] removeItemAtURL:url error:nil];
                                                 }];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten,
long long totalBytesWritten, long long totalBytesExpectedToWrite) {
   NSLog(@"bytesWritten - %d, totalBytesWritten - %lld,
     totalBytesExpectedToWrite - %lld", bytesWritten,
           totalBytesWritten, totalBytesExpectedToWrite);
}];
[client enqueueHTTPRequestOperation:operation];
} else {
NSError *error = [NSError errorWithDomain:kBRErrorDomainOwnDomain
                                     code:0



 userInfo:@{NSLocalizedDescriptionKey:kPreprocessingErrorUploadVideoMessage}];
    block(nil, error);
    }

Maybe someone knows the way how to fix it? Thanks for your help!

1条回答
在下西门庆
2楼-- · 2019-05-28 21:14

I had similar issue with AFNetworking: NSURLErrorDomain Code=-1001 "The request timed out." When retrieving from an external server and the device is on a subnet to a router connected to the WAN, the request works perfectly (192.168.1.0 subnet-1 -> WAN). However, if connected to a subnet which is connected to the router connected to the WAN, the request fails with the above message (192.168.0.0 subnet-2 -> 192.168.1.0 subnet-1 -> WAN). All browser operations work fine through the subnet-2, AFNetworking appears to connect but receives a time-out. I suspect the problem is with a configuration of the subnet-2 router.

查看更多
登录 后发表回答