转自:http://www.devdiv.com/afnetworking_-blog-21666-51451.html
热度 1|
NSURLRequest* request = [[YourHTTPClient sharedHTTPClient] multipartFormRequestWithMethod:@"POST"
path:path
parameters:dict
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data1
name:@"image1"
fileName:@"image1.jpg"
mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data2
name:@"image2"
fileName:@"image2.jpg"
mimeType:@"image/jpeg"];
}
}];
参见:
http://stackoverflow.com/questions/9559075/how-do-you-upload-multiple-files-with-afnetworking
另外参见官方wiki:
https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
How do I upload a file?
Uploading a file is really just constructing an HTTP multi-part form body for a URL request.
AFHTTPClient
provides several helper functions that make this easy.To create the request object, you would do something like:
NSData *imageData = UIImagePNGRepresentation(image); NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.png" mimeType:@"image/png"]; }];
You could then pass
request
into any request operation, likeAFJSONRequestOperation -JSONRequestOperationWithRequest:success:failure:
.