user975134
user975134

Reputation: 383

uploading image to server from iphone

After research I have figured out how to upload an image to my server like so

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"smiley1.jpg"], 90);
NSString *urlString = @"http://www.mysite.com/edit_profile.php";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

php:

$newname = "image01.jpg";
$place_file = move_uploaded_file( $_FILES['userfile']['tmp_name'], "members/$id/".$newname) or die("FAILED to Upload");

However, I don't want to use NSMutableURLRequest if I don't have to, with everything else in the app I have been using ASIFormDataRequest and I want to also use it to upload the image

Here is how I have tried, but failed:

NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/edit_profile.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"smiley1.jpg"], 90);
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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]];

[request setPostValue:contentType forKey:@"Content-Type"];
[request setPostBody:body];
[request setDelegate:self];
[request startAsynchronous];

Thanks in advance!!

Upvotes: 0

Views: 1665

Answers (1)

Alec Gorge
Alec Gorge

Reputation: 17410

ASIFormDataRequest has a built in way to handle file uploads.

Upvotes: 1

Related Questions