Search code examples
iosobjective-cafnetworking

Upload multiple Image or File using AFNetworking,


I want to upload multiple images using AFNetworking. like this Please Check This Image

i have attached example of the postman.

My Code :

NSString *key = [[mediaInfo allKeys] objectAtIndex:0];
NSDictionary *dict = [[mediaInfo objectForKey:key] objectAtIndex:0];
UIImage *image = [dict objectForKey:IQMediaImage];

NSMutableDictionary    *dictParam = [[NSMutableDictionary alloc] init];
[dictParam setValue:imageData forKey:@"Files"];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@BaseURL(@"/MediaUpload") parameters:dictParam progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {  }];

AFNetworking 3.0


Solution

  • Check this method from AFURLSessionManager:

    - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                         fromFile:(NSURL *)fileURL
                                         progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
                                completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
    

    Full code implementation:

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    [[sessionManager uploadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@BaseURL(@"/MediaUpload")]]
                                  fromFile:[NSURL fileURLWithPath:@"/path/to/uploading_file"]
                                  progress:^(NSProgress * _Nonnull uploadProgress) { }
                         completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    }] resume];