iPhone上传使用AFNetworking多部分文件 [英] iPhone upload multipart file using AFNetworking

查看:277
本文介绍了iPhone上传使用AFNetworking多部分文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS应用程序我要上传的文件使用 NSMutableURLRequest 分段文件的Java API。这里是表示参数的形式。

In my iOS app I want to upload file with the java API using NSMutableURLRequest for multipart file. here is the form which shows parameter.

  <form action="API_URL" encType='multipart/form-data' method=post>
            <input type=file name="files">
            <input type=submit value="Upload Attempt Files">

编辑窗口2

       <form action='URL' method="post" encType='multipart/form-data'>
<input name="key1" value='123'>
<input name="key2" value='asdf'>
<input name="key3" value='qwerty'>
<input name="key4" value='aaa'>
<input name="key5" value='aaa'>
<input name="key6" value='false'>
<input type="file"  name="files">
<input type=submit value="Create Forum Posts">
   </form>        

我怎样才能做到这一点。

How can I achieve that.

下面这个问题展示了如何使用的AFNetworking iOS中(客观C)。但我没有得到如何按表我使用把参数。

here this Question shows how to upload multipart file using AFNetworking in iOS(objective c). But I am not getting how to put parameter as per form I am using.

请帮忙,并建议

推荐答案

看你的HTML中,名称你的&LT的;输入类型=文件&gt; 文件,因此,你可以使用 @文件作为名称参数的<一个href=\"http://cocoadocs.org/docsets/AFNetworking/3.0.4/Protocols/AFMultipartFormData.html#//api/name/appendPartWithFileData:name:fileName:mimeType:\"><$c$c>appendPartWithFileData方法。例如, AFNetworking 3.X

Looking at your HTML, the name of your <input type=file> is files, and thus, you would use @"files" as the name parameter to the appendPartWithFileData method. For example, with AFNetworking 3.x:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData
                                name:@"files"
                            fileName:photoName mimeType:@"image/jpeg"];

    [formData appendPartWithFormData:[key1 dataUsingEncoding:NSUTF8StringEncoding]
                                name:@"key1"];

    [formData appendPartWithFormData:[key2 dataUsingEncoding:NSUTF8StringEncoding]
                                name:@"key2"];

    // etc.
} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"Response: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"Error: %@", error);
}];

(对于AFNetworking 1.x和2.x的语法,看到这个答案的修订历史记录。)

(For AFNetworking 1.x and 2.x syntax, see the revision history of this answer.)

这篇关于iPhone上传使用AFNetworking多部分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆