Note that there are some explanatory texts on larger screens.

plurals
  1. POPOST jpeg upload with AFNetworking
    text
    copied!<p>I can't for the life of me figure out why this doesn't work when I use AFNetworking. It worked with ASIHTTP. This is all very new to me. But I can't figure out why the files are not being transferred from $_FILES to the server's HD anymore. Here's the iOS code:</p> <pre><code>- (IBAction)uploadPressed { [self.fileName resignFirstResponder]; NSURL *remoteUrl = [NSURL URLWithString:@"http://mysite.com"]; NSTimeInterval timeInterval = [NSDate timeIntervalSinceReferenceDate]; NSString *photoName=[NSString stringWithFormat:@"%lf-Photo.jpeg",timeInterval]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // the path to write file NSString *filePath = [documentsDirectory stringByAppendingPathComponent:photoName]; NSData * photoImageData = UIImageJPEGRepresentation(self.remoteImage.image, 1.0); [photoImageData writeToFile:filePath atomically:YES]; NSLog(@"photo written to path: e%@", filePath); AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:remoteUrl]; NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/photos" parameters:nil constructingBodyWithBlock:^(id &lt;AFMultipartFormData&gt;formData) { [formData appendPartWithFormData:[self.fileName.text dataUsingEncoding:NSUTF8StringEncoding] name:@"name"]; [formData appendPartWithFileData:photoImageData name:self.fileName.text fileName:filePath mimeType:@"image/jpeg"]; } ]; 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]; } </code></pre> <p>I used to do this:</p> <pre><code>ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:remoteUrl]; [request setPostValue:self.fileName.text forKey:@"name"]; [request setFile:filePath forKey:@"filename"]; [request setDelegate:self]; [request startAsynchronous]; </code></pre> <p>Here's my PHP:</p> <pre><code> { // these could be stored in a .ini file and loaded // via parse_ini_file()... however, this will suffice // for an example $codes = Array( 100 =&gt; 'Continue', 101 =&gt; 'Switching Protocols', 200 =&gt; 'OK', 201 =&gt; 'Created', 202 =&gt; 'Accepted', 203 =&gt; 'Non-Authoritative Information', 204 =&gt; 'No Content', 205 =&gt; 'Reset Content', 206 =&gt; 'Partial Content', 300 =&gt; 'Multiple Choices', 301 =&gt; 'Moved Permanently', 302 =&gt; 'Found', 303 =&gt; 'See Other', 304 =&gt; 'Not Modified', 305 =&gt; 'Use Proxy', 306 =&gt; '(Unused)', 307 =&gt; 'Temporary Redirect', 400 =&gt; 'Bad Request', 401 =&gt; 'Unauthorized', 402 =&gt; 'Payment Required', 403 =&gt; 'Forbidden', 404 =&gt; 'Not Found', 405 =&gt; 'Method Not Allowed', 406 =&gt; 'Not Acceptable', 407 =&gt; 'Proxy Authentication Required', 408 =&gt; 'Request Timeout', 409 =&gt; 'Conflict', 410 =&gt; 'Gone', 411 =&gt; 'Length Required', 412 =&gt; 'Precondition Failed', 413 =&gt; 'Request Entity Too Large', 414 =&gt; 'Request-URI Too Long', 415 =&gt; 'Unsupported Media Type', 416 =&gt; 'Requested Range Not Satisfiable', 417 =&gt; 'Expectation Failed', 500 =&gt; 'Internal Server Error', 501 =&gt; 'Not Implemented', 502 =&gt; 'Bad Gateway', 503 =&gt; 'Service Unavailable', 504 =&gt; 'Gateway Timeout', 505 =&gt; 'HTTP Version Not Supported' ); return (isset($codes[$status])) ? $codes[$status] : ''; } function sendResponse($status = 200, $body = '', $content_type = 'text/html') { $status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status); header($status_header); header('Content-type: ' . $content_type); echo $body; } if (!empty($_FILES) &amp;&amp; isset($_POST["name"])) { $name = $_POST["name"]; $tmp_name = $_FILES['filename']['tmp_name']; $uploads_dir = '/var/www/cnet/photos'; move_uploaded_file($tmp_name, "$uploads_dir/$name.jpg"); $result = array("SUCCEEDED"); sendResponse(200, json_encode($result)); } else { sendResponse(400, 'Nope'); } ?&gt; </code></pre>
 

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