Question

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.

Was it helpful?

Solution

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

OTHER TIPS

http://loopj.com/android-async-http/ seems to be pretty easy to use for android networking including multipart (just like AFNetworking for IOS). The only thing I'm still struggling with, how to update a view (progress bar) while it's uploading. I also need to upload bunch of files synchronously (one at a time) which also seems to be not as easy as I thought (as least I have not found a solution for it yet). If anyone has any hints on using loopj asynchronously -- would be much appreciated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top