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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值