AFNetworking 表单请求(一个请求中上传多个文件) [英] AFNetworking Form Request (Multiple File uploads in One Request)

查看:38
本文介绍了AFNetworking 表单请求(一个请求中上传多个文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个 iOS 应用程序.该应用程序在一个请求中提供单个和多个文件上传选项.我正在使用 AFNetworking 进行单个文件上传,效果很好.现在,我们需要支持多文件上传.我有 html 代码,它实际上是从网络上传多个文件.我需要从 iOS 应用程序做同样的事情.谁能建议我们如何使用 AFNetworking 来做同样的事情?

We are developing an iOS application. The app is providing single as well as multiple file upload options in one single request. I'm using AFNetworking for single file uploads and which works fine. Now, we need to support multiple file upload. I have got the html code with me which actually does the multiple file uploads from the web.I need to do the same from iOS app. Can anybody suggest how do we do the same using AFNetworking?

<form method="post" action="upload.php?key=ac2a04cc7b6d4420fa5e5eb1701fb0cc1acf7916&channelid=1" enctype="multipart/form-data">
<label>Title:</label> <input name="title" value="My Video Title" /><br />
<label>Date:</label> <input name="date" value="<?php echo time(); ?>" /><br />
<label>Location:</label> <input name="location" value="Sydney, Australia" /><br />
<textarea name="description">The description of the photo/video goes here</textarea><br />
<input name="file[]" type="file" multiple="multiple" /><br />

推荐答案

使用下面的代码将多个文件发送到您的表单.如果需要更改任何内容,请根据您的需要进行自定义.

Use below code to send multiple file to your form. Customize as per your need if required to change anything.

  NSArray *imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"close_button.png"],[UIImage imageNamed:@"done_button.png"], nil];
    __block int i=1;
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:@"yoururl"];
    NSMutableURLRequest *request=nil;
        request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"yoururl" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
        {
            for(UIImage *eachImage in imageArray)
            {
                NSData *imageData = UIImagePNGRepresentation(eachImage);
                [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i] fileName:[NSString stringWithFormat:@"abc%d.png",i] mimeType:@"image/png"];
                i++;
            }

        }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSData *data = (NSData *)responseObject;
         NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
         NSLog(@"Response -> %@",str);

     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Error -> %@",[error localizedDescription]);
     }];

在服务器端,我检查了 test.php 代码,我刚刚打印了 print_r($_FILES) 并在我的情况下打印了以下内容,抱歉让您猜测.

On server side I have checked with test.php code, I have just printed print_r($_FILES) and it prints following in my case sorry to keep you guessing.

Response -> Array
(
    [file1] => Array
        (
            [name] => abc1.png
            [type] => image/png
            [tmp_name] => /tmp/phpiWbxSn
            [error] => 0
            [size] => 1997
        )

    [file2] => Array
        (
            [name] => abc2.png
            [type] => image/png
            [tmp_name] => /tmp/phpwtWTE8
            [error] => 0
            [size] => 1847
        )
)

希望这会有所帮助.

这篇关于AFNetworking 表单请求(一个请求中上传多个文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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