AFNetworking 一次上传多个文件的方法

转自:http://www.devdiv.com/afnetworking_-blog-21666-51451.html

热度 1已有 4693 次阅读 2013-6-30 01:51|系统分类:iOS

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:.

阅读更多
上一篇CornerStone配置SVN,HTTP及SVN简单使用说明
下一篇IOS数据库操作SQLite3使用详解
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭