我想在单个请求中上传多个图像,我设法在发送单个图像数据时使其工作。这是我发送单个图像数据的代码:
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseUrl: my_url ];
NSMutableRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil
parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: my_imageData name:@"image" fileName: @"myImage.jpg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request];
[operation start];
在服务器端,我使用Spring MVC。这是将接收包含多个图像的请求的Web服务/端点:
@RequestMapping(value="/test-multiple-upload/{collateralid}", method = RequestMethod.POST)
public @ResponseBody String multipartfilesUpload(
@PathVariable("collateralid") Long collId,
@RequestParam("images[]") CommonsMultipartFile[] images) {
//proccess the image data. save to local or to DB
return "uploaded successful";
}
我的问题是如何使用具有多个图像的AFNetworking制作multipartForm请求。
答案 0 :(得分:4)
我发现通过使用相同名称的附加数据将使数据成为服务器端的数组。因此:
NSArray *imageArray;
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseUrl: my_url ];
NSMutableRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil
parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
for(UIImage *img in imageArray)
{
[formData appendPartWithFileData: my_imageData name:@"image" fileName:@"myImage.jpg" mimeType:@"image/jpeg"];
}
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request];
[operation start];
考虑每个imageData将附加在表单数据多部分请求中,其名称标签与“图像”相同。 在服务器端,它将作为数组处理,“ CommonsMultipartFile [] image ”。