AFNetworking multiple files upload

2019-01-25 10:54发布

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"];

3条回答
不美不萌又怎样
2楼-- · 2019-01-25 11:01

For uploading any type of file to server, set mimeType as--> file/*

  [formData appendPartWithFileData:Data  
                           name:@"keyName" 
                           fileName:@"yourFileName"    
                           mimeType:@"file/*"];
查看更多
该账号已被封号
3楼-- · 2019-01-25 11:07

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){.....}
查看更多
干净又极端
4楼-- · 2019-01-25 11:10

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++;
}
}];
查看更多
登录 后发表回答