我正在使用 AFNetworking 上传多个文件。我需要为每个图像发送额外的信息,例如图像宽度/高度。
我以为我会遍历图像并发送它们,但 AFNetworking 并不能很好地处理它。
(我怀疑 AFNetworking 将多个请求合二为一,覆盖了额外的信息)
下面是我的代码。
NSURL *url = [NSURL URLWithString:@URL_BASE];
int count = [imageArray count];
for(int i = 0; i < count; ++i)
{
UIImage* image = [imageArray objectAtIndex:i];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: image.size.width], @"width",
[NSNumber numberWithFloat: image.size.height], @"height",
nil];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@PATH_ALBUM_IMAGE_UPLOAD parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d", i] fileName:@"avatar.jpg" mimeyTpe:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
[progressView setProgress: totalBytesWritten*1.0f / totalBytesExpectedToWrite animated: YES];
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
if(totalBytesWritten >= totalBytesExpectedToWrite)
{
progressView.hidden = YES;
}
}];
[operation start];
}