AFNetworking表单请求(在一个请求中上传多个文件)

时间:2013-04-08 12:16:34

标签: iphone ios upload afnetworking

我们正在开发iOS应用程序。该应用程序在一个请求中提供单个和多个文件上载选项。我正在使用AFNetworking进行单个文件上传,并且工作正常。现在,我们需要支持多个文件上传。我有我的html代码实际上是从web上传多个文件。我需要从iOS应用程序做同样的事情。任何人都可以建议我们如何使用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 />

2 个答案:

答案 0 :(得分:8)

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

  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),并且在我的情况下打印出来,抱歉让你猜不到。

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
        )
)

希望这有帮助。

答案 1 :(得分:0)

我尝试了AFHTTPNetwrok解决方案,但没有工作,然后我在“名称”键中添加方括号,如下所示,最后成功上传图像。我可能遇到什么问题?

NSData *imageData = UIImageJPEGRepresentation(img,1);
            [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"images[%d]",i] fileName:[NSString stringWithFormat:@"images%d.jpg",i] mimeType:@"image/jpeg"];