Reputation: 1868
I am using the following code to upload an image to my servlet.
NSData *data = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageNamed:@"jesus.png"])];
NSString *urlString = @"http://localhost:8080/MyServlet/MyServlet?filename=jesus.png";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"_187934598797439873422234";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"Content-Length %d\r\n\r\n", [data length] ] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
Its getting uploaded the image and saving this image into a particular mac desktop path. But, when i try to open that image, it says "The file couldn't be opened, it may damaged or use a file format that Preview doesn't recognize".
I saw the image file is having the correct size.
What could be the reason for not able to open this image file after uploaded from my iOs client? Could someone please suggest me?
Thank you.
Upvotes: 0
Views: 860
Reputation: 1868
It solved by having the code simple as below.
NSString *urlString = [NSString stringWithFormat:@"http://localhost:8080/MyServlet/MyServlet?filename=myimage"];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageNamed:@"jesus.png"])];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request addValue:@"image/png" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Ret: %@",returnString);
Upvotes: 0
Reputation: 1001
I think that you've got to start the body with the boundary, not with the content-length.
Try this:
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Length %d\r\n\r\n", [data length] ] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
Upvotes: 1