Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload Two images to WebServer
    text
    copied!<p>Im trying to upload two images to my web server. Heres what i do now, to upload one image :</p> <pre><code>NSData *imageData = UIImagePNGRepresentation(imageToSend); // setting up the URL to post to NSString *urlString = @"http://www.myweb.com.br/_resources/testeDir.php"; // setting up the request object now NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = @"14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; /* now lets create the body of the post */ NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // setting the body of the post to the reqeust [request setHTTPBody:body]; //Create the connection with the string URL and kick it off NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:request delegate:self]; [urlConnection start]; </code></pre> <p>With one image its works fine, but, how i can send two images at the same time?</p> <p>One option is to use AFNetworking, but i cant get it to work so far, heres what i tried :</p> <pre><code>AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:urlString]; NSMutableURLRequest *requestHTTP = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/" parameters:nil constructingBodyWithBlock: ^(id &lt;AFMultipartFormData&gt;formData) { [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"ipodfile.jpg" mimeType:@"image/jpeg"]; }]; AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:requestHTTP]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); //CGFloat progress = ((CGFloat)totalBytesWritten) / totalBytesExpectedToWrite * 100; }]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Upload succes"); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Upload failed"); }]; [operation start]; </code></pre> <p>Heres my php code :</p> <pre><code>&lt;?php $currentDirectory = getcwd(); $uploaddir = $currentDirectory."/4/HD/"; $uploaddir2 = $currentDirectory."/4/thumb/"; $file = basename($_FILES['userfile']['name']); $uploadfile = $uploaddir . $file; $file2 = basename($_FILES['userfile2']['name2']); $uploadfile2 = $uploaddir2 . $file2; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo $uploaddir."{$file}"; } if (move_uploaded_file($_FILES['userfile2']['tmp_name'], $uploadfile2)) { echo $uploaddir2."{$file2}"; } ?&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