关闭

提交表单 用AFNetWorking

标签: 提交表单
3132人阅读 评论(0) 收藏 举报
分类:

直接上代码:

这里//向服务器上传数据
-(void)importDate:(UIButton *)button {


    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_count,@"count",_productId,@"Id", nil];

    AFHTTPRequestOperationManager* mgr = [AFHTTPRequestOperationManager manager];


    //发送请求
    [mgr POST:E_returnGoodsDetailURL parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> totalformData) {



        for (int i = 0; i<_imageArr.count; i++) {

            [totalformData appendPartWithFileData:[_imageArr objectAtIndex:i] name:[NSString stringWithFormat:@"image%i",i] fileName:[NSString stringWithFormat:@"image%i.jpg",i] mimeType:@"image/jpeg"];
        }


    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //成功回调
        NSLog(@"成功回调====%@",responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //失败回调

    } ];



}

原理部分,从网上看到一个博主的文章:

1、网络请求中常用的有Get请求,Post请求,还有Head、Put、Delete、Options、Trace、Connect等
GET 获取指定资源
POST 向指定资源提交数据进行处理请求,在RESTful风格中用于新增资源
HEAD 获取指定资源头部信息
PUT 替换指定资源(不支持浏览器操作)
DELETE 删除指定资源
OPTIONS 允许客户端查看服务器的性能
TRACE 回显服务器收到的请求,主要用于测试或诊断
CONNECT 预留给能够将连接改为管道方式的代理服务器(HTTP代理使用)

2、上传图片在网页中最常用的就是POST请求了,将图片编码到POST请求体(body)中,通过请求数据一起发送到服务器上;

3、在iOS开发中,图片上传的请求体非常难写,格式要求非常严格,出一点错误都会造成上传失败,或请求数据失败,先看格式:
–Boundary+72D4CD655314C423
Content-Disposition: form-data; name=”uploadFile”; filename=”001.png”
Content-Type:image/png
Content-Transfer-Encoding: binary

… contents of boris.png …
–Boundary+72D4CD655314C423–
这是一个不带其他任务参数,body中只有一张图就要写成这样
分别说明一下:
–Boundary+72D4CD655314C423 // 分割符,以“–”开头,后面的字随便写,只要不写中文即可
Content-Disposition: form-data; name=”uploadFile”; filename=”001.png” // 这里注明服务器接收图片的参数(类似于接收用户名的userName)及服务器上保存图片的文件名
Content-Type:image/png // 图片类型为png
Content-Transfer-Encoding: binary // 编码方式
// 这里是空一行,必不可少!!
… contents of boris.png … // 图片数据部分
–Boundary+72D4CD655314C423– // 分隔符后面以”–”结尾,表明结束

3、如果有其他表单数据,如传用户ID等参数,也要写成以上格式:
–Boundary+72D4CD655314C423
Content-Disposition: form-data; name=”userId”

254
–Boundary+72D4CD655314C423
Content-Disposition: form-data; name=”shopId”

18718
具体参数含义与图片一致,一般这段会在图片上面,因此没有结尾符,如果没有其他数据了,那要加上结尾符。

4、代码示例:
- (void)upload
{
NSString *urlStr = @”http://localhost/upload.php“;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:0 timeoutInterval:5.0f];

[self setRequest:request];

NSLog(@"开始上传...");
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

    NSLog(@"Result--%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

}];

}

  • (void)setRequest:(NSMutableURLRequest *)request
    {
    NSString *boundary = [NSString stringWithFormat:@”Boundary+%08X%08X”, arc4random(), arc4random()];
    NSMutableData *body = [NSMutableData data];

    // 表单数据
    NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
    [param setValue:@”254” forKey:@”empId”];
    [param setValue:@”18718” forKey:@”shopId”];

    /** 遍历字典将字典中的键值对转换成请求格式:
    –Boundary+72D4CD655314C423
    Content-Disposition: form-data; name=”empId”

    254
    –Boundary+72D4CD655314C423
    Content-Disposition: form-data; name=”shopId”

    18718
    */
    [param enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSMutableString *fieldStr = [NSMutableString string];
    [fieldStr appendString:[NSString stringWithFormat:@”–%@\r\n”, boundary]];
    [fieldStr appendString:[NSString stringWithFormat:@”Content-Disposition: form-data; name=\”%@\”\r\n\r\n”, key]];
    [fieldStr appendString:[NSString stringWithFormat:@”%@”, obj]];
    [body appendData:[fieldStr dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@”\r\n” dataUsingEncoding:NSUTF8StringEncoding]];
    }];

    /// 图片数据部分
    NSMutableString *topStr = [NSMutableString string];
    NSString *path = [[NSBundle mainBundle] pathForResource:@”001.jpg” ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];

    /**拼装成格式:
    –Boundary+72D4CD655314C423
    Content-Disposition: form-data; name=”uploadFile”; filename=”001.png”
    Content-Type:image/png
    Content-Transfer-Encoding: binary

    … contents of boris.png …
    */
    [topStr appendString:[NSString stringWithFormat:@”–%@\r\n”, boundary]];
    [topStr appendString:@”Content-Disposition: form-data; name=\”uploadFile\”; filename=\”001.png\”\r\n”];
    [topStr appendString:@”Content-Type:image/png\r\n”];
    [topStr appendString:@”Content-Transfer-Encoding: binary\r\n\r\n”];
    [body appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:data];
    [body appendData:[@”\r\n” dataUsingEncoding:NSUTF8StringEncoding]];

    // 结束部分
    NSString *bottomStr = [NSString stringWithFormat:@”–%@–”, boundary];
    /**拼装成格式:
    –Boundary+72D4CD655314C423–
    */
    [body appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];

    // 设置请求类型为post请求
    request.HTTPMethod = @”post”;
    // 设置request的请求体
    request.HTTPBody = body;
    // 设置头部数据,标明上传数据总大小,用于服务器接收校验
    [request setValue:[NSString stringWithFormat:@”%d”, body.length] forHTTPHeaderField:@”Content-Length”];
    // 设置头部数据,指定了http post请求的编码方式为multipart/form-data(上传文件必须用这个)。
    [request setValue:[NSString stringWithFormat:@”multipart/form-data; boundary=%@”,boundary] forHTTPHeaderField:@”Content-Type”];
    }

如果用循环上传多张图片则:

NSMutableString *topStr = [NSMutableString string];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"001.jpg" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];

    /**拼装成格式:
     --Boundary+72D4CD655314C423
     Content-Disposition: form-data; name="uploadFile"; filename="001.png"
     Content-Type:image/png
     Content-Transfer-Encoding: binary

     ... contents of boris.png ...
     */
    [topStr appendString:[NSString stringWithFormat:@"--%@\r\n", boundary]];
    [topStr appendString:@"Content-Disposition: form-data; name=\"uploadFile\"; filename=\"001.png\"\r\n"];
    [topStr appendString:@"Content-Type:image/png\r\n"];
    [topStr appendString:@"Content-Transfer-Encoding: binary\r\n\r\n"];
    [body appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:data];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

写到for循环里面(图片的初始化以及请求头以及请求的转化,一定注意初始化也写到里面)

// 结束部分
    NSString *bottomStr = [NSString stringWithFormat:@"--%@--", boundary];
    /**拼装成格式:
     --Boundary+72D4CD655314C423--
     */
    [body appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];

    // 设置请求类型为post请求
    request.HTTPMethod = @"post";
    // 设置request的请求体
    request.HTTPBody = body;
    // 设置头部数据,标明上传数据总大小,用于服务器接收校验
    [request setValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField:@"Content-Length"];
    // 设置头部数据,指定了http post请求的编码方式为multipart/form-data(上传文件必须用这个)。
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];

(结束部分以及下面的请求写到循环体外面)

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:70526次
    • 积分:1731
    • 等级:
    • 排名:千里之外
    • 原创:100篇
    • 转载:54篇
    • 译文:0篇
    • 评论:3条
    最新评论