0

I came into an issue trying to send files with NSURLSessionUploadTask to a custom REST service. The problem is that the file seems to get transferred up to an arbitrary size, and then the task stops without any error (URLSession:task:didCompleteWithError: gets called with error set to nil).

The file that needs to be transferred is almost 10MB in size, and I found that smaller files sometimes get transferred correctly.

The code I'm using for creating the task is the following:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"100-continue" forHTTPHeaderField:@"Expect"];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", length] forHTTPHeaderField:@"Content-Length"];

NSURL *fileUrl = [NSURL fileURLWithPath:instanceFilePath];

NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileUrl];

I need to specify some headers for the server to correctly interpret the request, and the length variable is the effective size of the file being sent.

Any idea about what's going on here?

Thanks.

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
micamoita
  • 450
  • 2
  • 14
  • Have you logged the output of URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend: as you will be able to check if there is a mismatch in the content-length header that you're setting and what the delegate has determined to be the content-length – Sadiq Jaffer Aug 01 '14 at 10:20
  • Yes, I did. That's the method I use to track transmission progress. The totalBytesExpectedToSend parameter correctly matches the length of the file, but the totalBytesSent doesn't get past the 44% of it, in the tests I've made... – micamoita Aug 01 '14 at 10:50
  • Can you log the incoming connection on your REST service? – Sadiq Jaffer Aug 01 '14 at 10:53
  • Actually, the problem was on the server side. A size limit was set and the uploaded files exceeded that limit. Thank you! – micamoita Sep 02 '14 at 07:46

0 Answers0