Posted By

espinallab on 02/04/10


Tagged

images upload UIImage


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

bobbym245
bucko
Tintenklecks


Post a UIImage to the web


 / Published in: Objective C
 

URL: http://iphone.zcentric.com/?p=218

So here is something a lot of people have been wondering to do in the forums. How do I take a UIImage or any image and post it to a website. So this will go over how to do that.

There are two ways to tackle this issue. One we can base64 encode the file and post is normally inside a XML or JSON instance or we can simulate a normal HTML post. This tutorial will go over the HTML post.

  1. - (IBAction)uploadImage {
  2. /*
  3. turning the image into a NSData object
  4. getting the image back out of the UIImageView
  5. setting the quality to 90
  6. */
  7. NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
  8. // setting up the URL to post to
  9. NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";
  10.  
  11. // setting up the request object now
  12. NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
  13. [request setURL:[NSURL URLWithString:urlString]];
  14. [request setHTTPMethod:@"POST"];
  15.  
  16. /*
  17. add some header info now
  18. we always need a boundary when we post a file
  19. also we need to set the content type
  20.  
  21. You might want to generate a random boundary.. this is just the same
  22. as my output from wireshark on a valid html post
  23. */
  24. NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
  25. NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  26. [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  27.  
  28. /*
  29. now lets create the body of the post
  30. */
  31. NSMutableData *body = [NSMutableData data];
  32. [body appendData:[[NSString stringWithFormat:@"
  33. --%@
  34. ",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  35. [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"
  36. "] dataUsingEncoding:NSUTF8StringEncoding]];
  37. [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream
  38.  
  39. "] dataUsingEncoding:NSUTF8StringEncoding]];
  40. [body appendData:[NSData dataWithData:imageData]];
  41. [body appendData:[[NSString stringWithFormat:@"
  42. --%@--
  43. ",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  44. // setting the body of the post to the reqeust
  45. [request setHTTPBody:body];
  46.  
  47. // now lets make the connection to the web
  48. NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  49. NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
  50.  
  51. NSLog(returnString);
  52. }
  53.  
  54.  
  55.  
  56.  
  57. Here is the php function to grab the uploaded file:
  58.  
  59. $uploaddir = './uploads/';
  60. $file = basename($_FILES['userfile']['name']);
  61. $uploadfile = $uploaddir . $file;
  62.  
  63. if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  64. echo "http://iphone.zcentric.com/uploads/{$file}";
  65. }

Report this snippet  

You need to login to post a comment.