請求: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];
訣竅是用圖像[0],圖像1,圖像[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
)
)
)
希望這能解決您的問題.. :-)
@AppleDelegate編輯,使更適合於正確的縮進,大寫字母和白色空間,對於那些看到它的人來說更加可行。 – rptwsthi 2013-02-13 07:08:25
由於您的網絡服務一次只能接受一張圖片,因此您無法發送多個圖片,請不斷髮送您的網絡服務,直到您發送所有圖片或對該加載圖片數組進行一些修改或像image1,image2 ...等圖像。 – rptwsthi 2013-02-13 07:10:57
@rptwsthi我的代碼上有任何錯誤... – IKKA 2013-02-13 07:19:06