请求:ASIFormDataRequest 很旧,作者没有更新。您应该切换到AFNetworking。
如果您使用 PHP 作为后端以这种方式接收文件,您应该能够发送文件数组。
NSData *imageToUpload = UIImageJPEGRepresentation([UIImage imageNamed:@"profile-image-placeholder"], 10);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.imthi.com"]];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/info.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: imageToUpload name:@"image[0]" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData: imageToUpload name:@"image[1]" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData: imageToUpload name:@"image[2]" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _operation, id responseObject) {
NSString *response = [_operation responseString];
NSLog(@"response: [%@]",response);
} failure:^(AFHTTPRequestOperation * _operation, NSError *error) {
if([_operation.response statusCode] == 403){
NSLog(@"Upload Failed");
return;
}
}];
[operation start];
诀窍是使用 image[0]、image 1、image[2] 等作为添加更多文件的关键。尝试更改您的代码,它也应该适用于您当前的库。
[request addData:imgData withFileName:[NSString stringWithFormat:@"%@.jpg",dateString] andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"image[%d]",i]];
在您的 PHP 上,您应该收到这样的文件..
(
[image] => Array
(
[name] => Array
(
[0] => temp.jpeg
[1] => temp.jpeg
[2] => temp.jpeg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/php4XsVR8
[1] => /tmp/phpDBPzVO
[2] => /tmp/phpu26eZu
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 2207
[1] => 2207
[2] => 2207
)
)
)
希望这可以解决您的问题.. :-)