简体   繁体   中英

Upload Multiple Images using AFNetworking in IOS

I know its asked many times. But i'm facing some problem that i want to send multiple images to the server. The scenario is that , i have used a custom third party library for the selection of multiple images. After user select the images , the images comes in an array. The array are of two types, 1st array carries the name of images which user select no matters how many images he/she select and 2nd is the file path array of images , that array contains the array paths. I have three parameters while POST request 1st is name in which images name array comes, 2nd is ID in which id is pass to which these images are sent and third is images in which the images are. In my code i have used AFHTTPRequestOperationManager but i'm getting error on the header fie that this class had no header file in AFNetworkk folder which i used in my project. My code is this,

-(void)Images{

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager   alloc] initWithBaseURL:[NSURL URLWithString:@"My URL"]];
int i=0;
for(UIImage *eachImage in _arrai)
{
     NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"mainImage"]);
    [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"file%d.jpg",i ] mimeType:@"image/jpeg"];
    i++;
}

NSDictionary *parameters = @{@"name":_arrainame, @"property_id" : Propid, @"image" : _arrai};
AFHTTPRequestOperation *op = [manager POST:@"mainImage" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //do not put image inside parameters dictionary as I did, but append it!
    [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"cat1.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation operation, NSError error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];

}

NSDictionary *parameters = @{@"name":_arrainame, @"property_id" : Propid};

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    int i=0;
    for(NSString *eachImagePath in _arrai)
    {

        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:eachImagePath];
        [formData appendPartWithFileURL:fileURL name:@"imageParamter" fileName:[NSString stringWithFormat:@"file%d.jpg",i ] mimeType:@"image/jpeg" error:nil];
        i++;
    }
} error:nil];


AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
              uploadTaskWithStreamedRequest:request
              progress:nil
              completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                  if (error) {
                      NSLog(@"Error: %@", error);
                  } else {
                      NSLog(@"%@ %@", response, responseObject);
                  }
              }];

[uploadTask resume];

Try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM