2013-02-13 156 views
0

我無法上傳多個圖像到服務器。我正在使用以下代碼上傳:使用ASIHTTPRequest將多個圖像上傳到服務器上使用ASIHTTPRequest

-(void)upload { 
NSString *path=[NSString stringWithFormat:@"%@.php",webserviceURL]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:path]]; 
[request addPostValue:@"306" forKey:@"id"]; 

for (int i= 0; i<[imageArray count]; i++) { 
    NSData *imgData = UIImageJPEGRepresentation([imageArray objectAtIndex:i],1.0); 
    NSDate *date.... 

    ..... 

    NSLog(@"dateString");//for getting different file name.. 

    [request addData:imgData withFileName:[NSString stringWithFormat:@"%@.jpg",dateString] 
    andContentType:@"image/jpeg" forKey:@"image"]; 
    } 

[request setDelegate:self]; 
[request startAsynchronous]; 
} 

通過此代碼,我只能上傳一張圖片。如何使用相同的密鑰上傳多個圖像?

+0

@AppleDelegate編輯,使更適合於正確的縮進,大寫字母和白色空間,對於那些看到它的人來說更加可行。 – rptwsthi 2013-02-13 07:08:25

+0

由於您的網絡服務一次只能接受一張圖片,因此您無法發送多個圖片,請不斷髮送您的網絡服務,直到您發送所有圖片或對該加載圖片數組進行一些修改或像image1,image2 ...等圖像。 – rptwsthi 2013-02-13 07:10:57

+0

@rptwsthi我的代碼上有任何錯誤... – IKKA 2013-02-13 07:19:06

回答

3

請求: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 
      ) 

    ) 

希望這能解決您的問題.. :-)

+0

我們班導入了哪一類AFNetworking – 2013-12-01 13:56:14

相關問題