Search code examples
iphoneuploadios-simulatorasihttprequest

ASIFormDataRequest upload video works in iPhone simulator but fails with iPhone for files over 1.5 MB


I am using ASIHTTPFormDataRequest to upload a video file. I know my code works because when I upload a video under 1.5 MB (low quality under 1 minute 40 seconds) on the iPhone it successfully posts. I was convinced it was a server issue so we did every test ever and came back with no success. It wasn't until the other day that I was able to successfully upload a large file within the iPhone simulator. The file was over 5 MB and high quality. So I am forced to belive that it has something to do with the iPhone device itself. I've googled up a storm and came back with the same stupid response: its a server issue. Well its not...

Here is my code:

-(void) post:(NSData *) pVideoData progressBarView:(UIProgressView *) pProgressBarView{ 

    pVideoData = [NSData dataWithContentsOfFile: m_pAppDelegate.m_pAppData.m_pPhotoURL]; 
  NSString *urlAddress = [NSString stringWithString:@"https://api.sitename.com/action/upload"];
NSURL *url = [NSURL URLWithString:urlAddress];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];


[request setPostValue:API_KEY forKey:@"api_key"];
[request setPostValue: m_pAppDelegate.m_pAppData.m_sTokenKey forKey:@"user_token"];


[request setPostValue: @"somename" forKey:@"tag"];
[request setPostValue: [AppData getIPAddress] forKey:@"ip"];



[request setData:pVideoData forKey:@"file"];


request.numberOfTimesToRetryOnTimeout = 3;
[request setDelegate:self];
  [request showAccurateProgress];

[request setShouldStreamPostDataFromDisk:YES];
[request setDidFinishSelector:@selector(postRequestSuccess:)];
[request setDidFailSelector:@selector(postRequestFailed:)];
[request startAsynchronous];
    [request setUploadProgressDelegate: pProgressBarView];}

Everything works fine. Please note that I have already tried the following: synchronous download, & setfile. But I don't belive that the problem lies in my code or my server.

Here is the data I get while uploading: please note the differences between the two.

SUCCESSFUL VIDEO UPLOAD:

2012-03-13 14:31:06.413 MYAPP[7805:707] [STATUS] Starting asynchronous request <ASIFormDataRequest: 0xb9bc00>
2012-03-13 14:31:06.543 MYAPP[7805:8c07] 
==== Building a multipart/form-data body ====
--0xKhTmLbOuNdArY-26765320-0965465416-4395-BA24-AA5CF2B58A66
Content-Disposition: form-data; name="api_key"

*SECRET    --0xKhTmLbOuNdArY-26765320-0916-4395-BA24-AA5CF2B58A66
Content-Disposition: form-data; name="user_token"

Br300MCw4P-l06SFITFCHdiifBHdOCPczX0Y830Yfabox3wLMPs2s7MlWTAS7F2TlwuhL2kiZ7mEXeDWkmDi5g
--0xKhTmLbOuNdArY-26765320-0916-4395-BA24-AA5CF2B58A66
Content-Disposition: form-data; name="tag"

NPSH Live
--0xKhTmLbOuNdArY-26765320-0916-4395-BA24-AA5CF2B58A66
Content-Disposition: form-data; name="ip"

61.197.151.129
--0xKhTmLbOuNdArY-26765320-0916-4395-BA24-AA5CF2B58A66
Content-Disposition: form-data; name="longitude"

-122.357832
--0xKhTmLbOuNdArY-26765320-0916-4395-BA24-AA5CF2B58A66
Content-Disposition: form-data; name="latitude"

47.781178
--0xKhTmLbOuNdArY-26765320-0916-4395-BA24-AA5CF2B58A66
Content-Disposition: form-data; name="file"; filename="file"
Content-Type: application/octet-stream

[920870 bytes of data]
--0xKhTmLbOuNdArY-26765320-0916-4395-BA24-AA5CF2B58A66--
==== End of multipart/form-data body ====
2012-03-13 14:31:06.550 MYAPP[7805:8c07] [CONNECTION] Request <ASIFormDataRequest: 0xb9bc00> will not use a persistent connection
2012-03-13 14:31:06.819 MYAPP[7805:8c07] [THROTTLING] ===Used: 238 bytes of bandwidth in last measurement period===
wait_fences: failed to receive reply: 10004003
2012-03-13 14:31:07.992 MYAPP[7805:8c07] [THROTTLING] ===Used: 327680 bytes of bandwidth in last measurement period===
2012-03-13 14:31:09.053 MYAPP[7805:8c07] [THROTTLING] ===Used: 655360 bytes of bandwidth in last measurement period===
2012-03-13 14:31:09.820 MYAPP[7805:8c07] [STATUS] Request <ASIFormDataRequest: 0xb9bc00> finished uploading data
2012-03-13 14:31:10.070 MYAPP[7805:8c07] [THROTTLING] ===Used: 860720 bytes of bandwidth in last measurement period===
2012-03-13 14:31:11.320 MYAPP[7805:8c07] [THROTTLING] ===Used: 0 bytes of bandwidth in last measurement period===
2012-03-13 14:31:11.835 MYAPP[7805:8c07] [STATUS] Request <ASIFormDataRequest: 0xb9bc00> received response headers
2012-03-13 14:31:11.843 MYAPP[7805:8c07] [STATUS] Request <ASIFormDataRequest: 0xb9bc00> finished downloading data (105 bytes)
2012-03-13 14:31:11.854 MYAPP[7805:8c07] [STATUS] Request finished: <ASIFormDataRequest: 0xb9bc00>

FAILED UPLOAD:

[STATUS] Starting asynchronous request <ASIFormDataRequest: 0xb8e200>
wait_fences: failed to receive reply: 10004003
2012-03-13 14:21:03.093 MYAPP[7805:8c07] 
==== Building a multipart/form-data body ====
--0xKhTmLbOuNdArY-E6B28382AE4C-F123-499E-8076-19CC8D41F46E
Content-Disposition: form-data; name="api_key"

*secret
--0xKhTmLbOuNdArY-E6B2AE4C-F123-499E-8076-19CC8D41F46E
Content-Disposition: form-data; name="user_token"

Br300MCw4P-l06SFITFCHdiifBHdOCPczX0Y830Yfabox3wLMPs2s7MlWTAS7F2TlwuhL2kiZ7mEXeDWkmDi5g
--0xKhTmLbOuNdArY-E6B2AE4C-F123-499E-8076-19CC8D41F46E
Content-Disposition: form-data; name="tag"

test
--0xKhTmLbOuNdArY-E6B2AE4C-F123-499E-8076-19CC8D41F46E
Content-Disposition: form-data; name="ip"

81.197.151.699
--0xKhTmLbOuNdArY-E6B2AE4C-F123-499E-8076-19CC8D41F46E
Content-Disposition: form-data; name="longitude"

-122.357874
--0xKhTmLbOuNdArY-E6B2AE4C-F123-499E-8076-19CC8D41F46E
Content-Disposition: form-data; name="latitude"

47.781192
--0xKhTmLbOuNdArY-E6B2AE4C-F123-499E-8076-19CC8D41F46E
Content-Disposition: form-data; name="file"; filename="file"
Content-Type: application/octet-stream

[3280300 bytes of data]
--0xKhTmLbOuNdArY-E6B2AE4C-F123-499E-8076-19CC8D41F46E    --
==== End of multipart/form-data body ====
2012-03-13 14:21:03.099 MYAPP[7805:8c07] [CONNECTION] Request <ASIFormDataRequest: 0xb8e200> will not use a persistent connection
2012-03-13 14:21:03.354 MYAPP[7805:8c07] [THROTTLING] ===Used: 2500 bytes of bandwidth in last measurement period===
2012-03-13 14:21:04.605 MYAPP[7805:8c07] [THROTTLING] ===Used: 0 bytes of bandwidth in last measurement period===
2012-03-13 14:21:05.698 MYAPP[7805:8c07] [THROTTLING] ===Used: 458752 bytes of bandwidth in last measurement period===
2012-03-13 14:21:06.744 MYAPP[7805:8c07] [THROTTLING] ===Used: 688128 bytes of bandwidth in last measurement period===
2012-03-13 14:21:07.755 MYAPP[7805:8c07] [THROTTLING] ===Used: 1114112 bytes of bandwidth in last measurement period===
2012-03-13 14:21:08.855 MYAPP[7805:8c07] [THROTTLING] ===Used: 950272 bytes of bandwidth in last measurement period===
2012-03-13 14:21:09.897 MYAPP[7805:8c07] [THROTTLING] ===Used: 458752 bytes of bandwidth in last measurement period===
2012-03-13 14:21:11.050 MYAPP[7805:8c07] [THROTTLING] ===Used: 393216 bytes of bandwidth in last measurement period===
2012-03-13 14:21:12.085 MYAPP[7805:8c07] [THROTTLING] ===Used: 917504 bytes of bandwidth in last measurement period===
2012-03-13 14:21:13.104 MYAPP[7805:8c07] [THROTTLING] ===Used: 458752 bytes of bandwidth in last measurement period===
2012-03-13 14:21:13.858 MYAPP[7805:8c07] [STATUS] Request <ASIFormDataRequest: 0xb8e200> finished uploading data
2012-03-13 14:21:14.354 MYAPP[7805:8c07] [THROTTLING] ===Used: 1123122 bytes of bandwidth in last measurement period===
2012-03-13 14:21:15.014 MYAPP[7805:8c07] [STATUS] Request <ASIFormDataRequest: 0xb8e200> received response headers
2012-03-13 14:21:15.017 MYAPP[7805:8c07] [STATUS] Request <ASIFormDataRequest: 0xb8e200> finished downloading data (76 bytes)
2012-03-13 14:21:15.034 MYAPP[7805:8c07] [STATUS] Request finished: <ASIFormDataRequest: 0xb8e200>

So what do you guys think the problem could be. Another note: I have tried using other wifi connections as well as turning off wifi, using 3g and turning off 3g and only using wifi. Do you think there are limits to how much a user can upload on the iPhone?

ONE LAST NOTE:

The upload apparently is successful, but I guess the file I upload becomes invalid all together. My server responds successful by telling my that everything was set and okay but the video file itself. The video file is invalid. Yet it works in the simulator...

EDIT!!

I JUST FINISHED TRYING OUT A DIFFERENT METHOD FOR MY UPLOADING, AND I GET THE EXACT SAME ERRORS FOR THE EXACT SAME CIRCUMSTANCES SO I AM STARTING TO THINK THAT IT HAS SOMETHING TO DO WITH MY CORE FILES? MAYBE ITS A MEMORY THING?

 NSURL *remoteUrl = [NSURL URLWithString:@"http://api.mysite.com/upload"];

NSURL *myfile = [NSURL URLWithString:m_pAppDelegate.m_pAppData.m_PendingInfo.m_pMovieURL];


NSData * photoImageData = [NSData dataWithContentsOfFile: m_pAppDelegate.m_pAppData.m_PendingInfo.m_pMovieURL];
NSError **error;



  // [photoImageData writeToFile:filePath atomically:YES];

   AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:remoteUrl];
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" 
                                                                       path:@"" 
                                                                 parameters:nil 
                                                  constructingBodyWithBlock:^(id <AFMultipartFormData>formData) 
                                  {
                                      [formData appendPartWithFormData:[API_KEY dataUsingEncoding:NSUTF8StringEncoding] 
                                                                  name:@"api_key"];
                                      [formData appendPartWithFormData:[m_pAppDelegate.m_pAppData.m_sTokenKey dataUsingEncoding:NSUTF8StringEncoding] 
                                                                  name:@"user_token"];


                                      [formData appendPartWithFileData:photoImageData 
                                                                  name:@"file" 
                                                              fileName:@"test.mov" 
                                                              mimeType:@"video/quicktime"]; 
                                      [formData appendPartWithFileURL:myfile name:@"file" error:error];
                                  }
                                  ];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {

    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);

}];

[operation setCompletionBlock:^{
    NSLog(@"%@", operation.responseString); //Gives a very scary warning
}];

[operation start];   

Solution

  • Turned out to be a problem in my .htpconfig file. I had a hard set limit set on it. Adjusting it worked like a charm!