AFNetworking 多文件上传

Posted

技术标签:

【中文标题】AFNetworking 多文件上传【英文标题】:AFNetworking multiple files upload 【发布时间】:2013-08-28 08:17:40 【问题描述】:

我想将一些图像上传到服务器,所以我使用 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"];

【问题讨论】:

【参考方案1】:

将变量i 放入块中。还将文件名从abc 更改为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++;

];

【讨论】:

【参考方案2】:

只需更改 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).....

【讨论】:

如果我想要一个 name 可以包含多个文件?【参考方案3】:

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

  [formData appendPartWithFileData:Data  
                           name:@"keyName" 
                           fileName:@"yourFileName"    
                           mimeType:@"file/*"];

【讨论】:

你回答的不是我问的。

以上是关于AFNetworking 多文件上传的主要内容,如果未能解决你的问题,请参考以下文章

AFNetworking 多文件上传

使用AFnetworking以多部分格式上传图像在ios中不起作用

AFNetworking 2.0 多部分/表单数据上传到 mySQL

使用 afnetworking 2.1.0 实现“多部分请求的上传任务”时出错

获取 System.InvalidOperationException:请求格式无效:通过 AFNetworking 上传图像(数据)时出现多部分/表单数据错误

[iOS]服务器使用php时多图片上传AFNetworking的设置