2011-01-13 117 views
2

我必須上傳圖像到服務器形式iPhone我使用ASIHTTPRequest。我已經設置了一個循環來上傳七個文件。但是在執行完最後一個文件之後,有人可以指出我錯誤的地方。上傳圖像到服務器使用ASIHTTPRequest在iPhone

我使用下面的代碼上傳:

for (int i=1; i<8; i++) 
    { 
     NSString* filename = [NSString stringWithFormat:@"Photo%d.jpg", i]; 
     NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:filename]; 
     [request setFile:path forKey:[NSString stringWithFormat:@"file"]]; 
    } 

    [request startAsynchronous]; 
    [resultView setText:@"Uploading data..."]; 

My Php file code is as following : 



<?php 
     if ($_FILES["file"]["error"] > 0) 
     { 
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
     } 
     else 
     { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 

     if (file_exists("vinay/" . $_FILES["file"]["name"])) 
      { 
      echo $_FILES["file"]["name"] . " already exists. "; 
      } 
     else 
      { 
      move_uploaded_file($_FILES["file"]["tmp_name"], 
      "vinay/" . $_FILES["file"]["name"]); 
      echo "Stored in: " . "http://serverpath" . $_FILES["file"]["name"]; 
      } 
     } 
    ?> 

回答

2

您正在覆蓋名爲文件&的密鑰,您需要使用隊列。

[self setNetworkQueue:[ASINetworkQueue queue]]; 
[[self networkQueue] setDelegate:self]; 

for (int i=1; i<8; i++) 
{ 
    NSString* filename = [NSString stringWithFormat:@"Photo%d.jpg", i]; 
    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:filename]; 
    [request setFile:path forKey:[NSString stringWithFormat:@"file%d", i]]; 
    [[self networkQueue] addOperation:request]; 
} 
[[self networkQueue] go]; 
+0

嗨我試過你的選擇,但這次沒有上傳文件。如果這造成了一些問題,我還附加了我的PHP文件代碼。 – 2011-01-13 13:51:16

1

你覆蓋request.file,因此只有最後一個被上傳。您必須爲每個文件提出個別請求。

或者您可以使用[request addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key]在一個請求中發送多個文件。

+0

我要上傳多個文件(文件數未證實)現在怎麼設置的要求,使我們可以採用這種帶環。 – 2011-01-13 13:31:51