views:

46

answers:

1

I'm trying to send a single image from my iPhone app to my Google App Engine Java server. On the server, I'm getting a FileUploadBase$InvalidContentTypeException, which is thrown when the request is not a multipart request. ServletFileUpload.isMultipartContent(req) is printing false. I'm trying to send an image, encoded as a jpeg, and a few other parameters, but im testing with just "name" and the image. There are a few fragmented sources of info online demonstrating how to take an image from the iPhone and post it to a GAE server. I'd like to document both ends of the process here.

Here's my objective-c code:

NSData *imageData = [NSData dataWithData: UIImageJPEGRepresentation(theImageView.image, 1.0f)];
NSURL *url = [NSURL URLWithString:@"http://myurl.com/myservlet"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithString:@"Content-Type: multipart/form-data; boundary=\"6G+f\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

 [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"name\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"whatever"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"myImage\" filename=\"ipodfile.jpeg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData: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 *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

 NSLog(@"return %@",returnString);

I took most of the java code from here: http://stackoverflow.com/questions/1513603/how-to-upload-and-store-an-image-with-google-app-engine-java . I'll post the complete solution once the objective-c problem is found. THANKS!!

+1  A: 

Scrap all that code and start using ASIHTTPRequest for your http client needs. It's SO much easier and more powerful. I build highly server-interactive apps that include image uploads, and I haven't touched the native NSURLConnection interface in literally months.

That library has an ASIFormDataRequest object that handles all the fiddly little pieces of configuration for you. You literally load it up with data, fire it, and it works.

http://allseeing-i.com/ASIHTTPRequest/How-to-use

Dan Ray
I did this. I tried doing it yesterday but there was a conflict with Reachability (a different part of the app was using 1.5 and didnt like the new version, which is apparently required for ASIHTTPRequest). I just included both versions under different names and everythings working perfectly now. Thanks
culov