9

我想将一些图像上传到服务器,所以我使用 AFNetWork 发布文件。代码在这里:

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){.....}

这不行!但是当我使用下面的代码替换 multipartFormRequestWithMethod 方法中的“for”语句时,一切正常!但是如果我无法确认图像数组的计数,我必须使用“for”语句,我的代码有问题吗?

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

3 回答 3

14

将变量i放入块中。还将文件名从 更改abcfile

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++;
}
}];
于 2013-08-28T08:27:30.993 回答
4

只需更改 for 循环语法

 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){.....}
于 2013-08-28T08:36:50.927 回答
-3

要将任何类型的文件上传到服务器,请将 mimeType 设置为 --> file/*

  [formData appendPartWithFileData:Data  
                           name:@"keyName" 
                           fileName:@"yourFileName"    
                           mimeType:@"file/*"];
于 2014-11-15T12:35:32.733 回答