Im struggling to find a solution or library with good documentation describing how to submit a multipart image to a server. I had a look at loopJ, which I think is very promising however I am still unsure what the best solution is. It would be great if someone could give me advice/strategy or code example I have used in my iOS, (AFNetworking
) shown below:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"userID": @"1234567890"};
// add parameters first (user id) and then multipart image
[manager POST:MAIN_URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// add multipart image
[formData appendPartWithFileData:imageData name:@"uploadFile" fileName:@"uploadFile" mimeType:@"image/jpg"];
} success:^(AFHTTPRequestOperation *operation,
id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation,
NSError *error) {
NSLog(@"Error: %@", error);
}];
Where imageData
is the Image that needs to be uploaded.
I used the Apache's Multipart Entity Builder. Then just convert your image into a byte array or input stream.
http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html