Note that there are some explanatory texts on larger screens.

plurals
  1. POAFNetworking upload image working intermittently
    primarykey
    data
    text
    <p>I'm using AFNetworking to upload an image along with a few parameters to a PHP script (built with CodeIgniter) which will receive the image, place the file name and parameters in a database and move the image to a permanent location.</p> <p>Here's the Obj-C:</p> <pre><code>NSURL *url = [NSURL URLWithString:@"http://my_api_endpoint"]; NSData *imageToUpload = UIImageJPEGRepresentation(_mainMedia, .25f); NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys: _topicText.text,@"Topic", @"1",@"Category", @"1",@"Creator", nil]; AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:url]; NSString *timeStamp = [NSString stringWithFormat:@"%0.0f.jpg", [[NSDate date] timeIntervalSince1970]]; NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"apidebate/debates" parameters:params constructingBodyWithBlock: ^(id &lt;AFMultipartFormData&gt;formData) { [formData appendPartWithFileData: imageToUpload name:@"MainMedia" fileName:timeStamp mimeType:@"image/jpeg"]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *response = [operation responseString]; NSLog(@"response: [%@]",response); [self dismissViewControllerAnimated:YES completion:nil]; [self.delegate createTopicViewControllerDidCreate:self]; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; if([operation.response statusCode] == 403){ NSLog(@"Upload Failed"); return; } NSLog(@"error: %@", [operation error]); [self dismissViewControllerAnimated:YES completion:nil]; [self.delegate createTopicViewControllerDidCreate:self]; }]; [operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); float width = totalBytesWritten / totalBytesExpectedToWrite; }]; [operation start]; </code></pre> <p>Here's the PHP:</p> <pre><code>//CONTROLLER FROM API function debates_post() { mail('myemailaddress@gmail.com', 'Test', 'Posted'); $tmp_dir = "images/posted/"; if(isset($_FILES['MainMedia'])){ $SaniFileName = preg_replace('/[^a-zA-Z0-9-_\.]/','', basename($_FILES['MainMedia']['name'])); $file = $tmp_dir . $SaniFileName; move_uploaded_file($_FILES['MainMedia']['tmp_name'], $file); } else $SaniFileName = NULL; $data = array('Topic'=&gt;$this-&gt;post('Topic'), 'MainMedia'=&gt;$this-&gt;post('MainMedia'), 'Category'=&gt;$this-&gt;post('Category'), 'Creator'=&gt;$this-&gt;post('Creator')); $insert = $this-&gt;debate-&gt;post_debate($this-&gt;post('Topic'), $SaniFileName, $this-&gt;post('Category'), $this-&gt;post('Creator')); if($insert){ $message = $this-&gt;db-&gt;insert_id(); } else{ $message = 'Insert failed'; } $this-&gt;response($message, 200); } //MODEL function post_debate($Topic=NULL, $MainMedia='', $Category=NULL, $Creator=NULL){ $MainMedia = ($MainMedia)?$MainMedia:''; $data = array( 'Topic' =&gt; $Topic, 'MainMedia' =&gt; $MainMedia, 'Category' =&gt; $Category, 'Creator' =&gt; $Creator, 'Created' =&gt; date('Y-m-d h:i:s') ); return $this-&gt;db-&gt;insert('debate_table', $data); } </code></pre> <p>My current problem is that the upload from iOS very rarely completes and there is no pattern to when it does not. I can add large or small photos or no photo at all with just parameters and it works 20% of the time. When it fails, this is the error message I get in X-Code:</p> <pre><code>2012-08-26 01:52:10.698 DebateIt[24215:907] error: Error Domain=NSURLErrorDomain Code=-1021 "request body stream exhausted" UserInfo=0x1dd7a0d0 {NSErrorFailingURLStringKey=http://my_api_url, NSErrorFailingURLKey=http://my_api_url, NSLocalizedDescription=request body stream exhausted, NSUnderlyingError=0x1e8535a0 "request body stream exhausted"} </code></pre> <p>What in the world is this? </p> <p>I have a basic HTML form that I post the same image with to the same endpoint and it works every time with any size image. iOS seems to allow images of varying size just fine...but large or small, it will probably only work on the second try.</p> <p>Thoughts?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload