NSDictionary *dic = @{@"key":@"value"}; //如只传图片 此处可以省略 下面方法的parameters可以为nil
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"url" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (int i = 0;i<photoArr.count;i++){ //photoArr是存储相册里面的图片 类型为image
UIImageView *img = [UIImageView new];
img.image = photoArr[i];
NSData *data = UIImagePNGRepresentation(img.image);
NSString *filename = [NSString stringWithFormat:@"img%d.png",i];
[formData appendPartWithFileData:data name:@"image" fileName:filename mimeType:@"image/png"]; //此处注意name是POST方法的KEY,是服务器提供的参数名
}
} error:nil];
AFHTTPRequestOperation *req = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[req setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"%d-------%d-------%d",bytesWritten,totalBytesWritten,totalBytesExpectedToWrite);
}];
[req setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; //此处要用UTF8解码
NSLog(@"%@",str);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[req start];