使用下面的代码将多个文件发送到您的表单。如果需要更改任何内容,请根据您的需要进行自定义。
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
)
)
希望这可以帮助。