Uploading an image using Multipart and posting multipart object as a param to spring mvc controller

StackOverflow https://stackoverflow.com/questions/19977972

Question

Hi I have a backend that accepts a multipart object. I'm struggling to find direction/necessary steps involved to post an image as a multipart object and passing it as a param to a spring mvc controller. I've searched the web for countless hours but am still struggling to get to grasps with it.

Basically I am an ios developer and we have a spring backend. I need to send an image as a multipart object because it is needed as a parameter to be stored in the backend.

I was reading about AFNetworking but I am still unsure.The backend was created before the app was designed (the backend was contracted out) so now I am trying to connect to what is there.

I can supply more information if needed.

I tried the code below by Anand K but now I'm getting HTTP Status 500 and this in the response:

Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException: 
The current request is not a multipart request

as well as some other 'junk'

new Update based on Adnan Ks suggestion *

UIImage *image=[UIImage imageNamed:@"dub.png"];
NSData *imageData=UIImagePNGRepresentation(image);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://website.com/upload/uploadOneBook.app?userID=12345&uploadFile="]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"attachement\"; filename=\"%@\";",@"Image.png" ] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\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(@"returnString=%@",returnString);

And the returnString contains: `HTTP Status 400 - Required MultipartFile parameter 'uploadFile' is not present

Was it helpful?

Solution

I opted for AFNetworking which had a very good example for multipart objects.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];

[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
      [formData appendPartWithFileURL:filePath name:@"image" error:nil];
     } success:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSLog(@"Success: %@", responseObject);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
 }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top