Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking multiple files upload

I want to upload some images into the server,so I use AFNetWork to post the files. The code is here:

UIImage *image1 = [UIImage imageNamed:@"about_app"];
UIImage *image2 = [UIImage imageNamed:@"alter"];
NSArray *array = @[image1,image2];
__block int i = 0;
NSMutableURLRequest *request = [[AFNetWorkSingleton shareInstance] multipartFormRequestWithMethod:@"POST" path:@"Mindex/getimg" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>formData){
for(UIImage *eachImage in array)
{
    NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
    [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"abc%d.jpg",i ] mimeType:@"image/jpeg"];
    i++;
}
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){.....}

this is not work! But when I use the code below to replace the "for" statement inside multipartFormRequestWithMethod method, all things went right! but if I couldn't confirm the count of image array,I must use "for" statement,something wrong with my code?

 [formData appendPartWithFileData:UIImageJPEGRepresentation([array objectAtIndex:0], 0.5) name:@"image1" fileName:@"image1.jpg" mimeType:@"image/jpeg"];
 [formData appendPartWithFileData:UIImageJPEGRepresentation([array objectAtIndex:1], 0.5) name:@"image2" fileName:@"image2.jpg" mimeType:@"image/jpeg"];
like image 467
lixiaoyu Avatar asked Aug 28 '13 08:08

lixiaoyu


2 Answers

Put the variable i inside the block. Also changed the name of file from abc to file.

UIImage *image1 = [UIImage imageNamed:@"about_app"];
UIImage *image2 = [UIImage imageNamed:@"alter"];
NSArray *array = @[image1,image2];

NSMutableURLRequest *request = [[AFNetWorkSingleton shareInstance] multipartFormRequestWithMethod:@"POST" path:@"Mindex/getimg" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>formData){
int i = 0;
for(UIImage *eachImage in array)
{
    NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
    [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"file%d.jpg",i ] mimeType:@"image/jpeg"];
    i++;
}
}];
like image 171
Apurv Avatar answered Oct 03 '22 05:10

Apurv


just change the for loop syntax

 UIImage *image1 = [UIImage imageNamed:@"about_app"];
    UIImage *image2 = [UIImage imageNamed:@"alter"];
    NSArray *array = @[image1,image2];

    NSMutableURLRequest *request = [[AFNetWorkSingleton shareInstance] multipartFormRequestWithMethod:@"POST" path:@"Mindex/getimg" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>formData){
    for(int i=0;i<[array count];i++)
    {
        UIImage *eachImage  = [array objectAtIndex:i];
        NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
        [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"abc%d.jpg",i ] mimeType:@"image/jpeg"];
    }
    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){.....}
like image 35
Pradeep Avatar answered Oct 03 '22 05:10

Pradeep