2010-12-06 25 views
0

我想使用ASIFormDataRequest將文件上傳到服務器(從自定義mac os應用程序)。在我的可可/ OBJ-C代碼,我有:ASIFormDataRequest + PHP問題

- (IBAction)uploadFile:(id)sender 
{ 
[networkQueue reset]; 
[networkQueue setShowAccurateProgress:YES]; 
[networkQueue setUploadProgressDelegate:progressIndicator]; 
[networkQueue setRequestDidFailSelector:@selector(postFailed:)]; 
[networkQueue setRequestDidFinishSelector:@selector(postFinished:)]; 
[networkQueue setDelegate:self]; 

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://domain.com/to/upload.php"]] autorelease]; 

[request setFile:@"/path/to/file.jpg" forKey:@"file"]; 

[networkQueue addOperation:request]; 
[networkQueue go]; 

} 

- (void)postFinished:(ASIHTTPRequest *)request 
{ 
NSLog(@"Post Success"); 
} 
- (void)postFailed:(ASIHTTPRequest *)request 
{ 
NSLog(@"Post Failed"); 
} 

在服務器上的PHP代碼如下所示:

$target_path = "files/"; 

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 

當我嘗試使用此代碼上傳文件,我得到一個「郵寄成功」響應在客戶端,但該文件永遠不會顯示在我的服務器上。爲了以防萬一,我將CHMODED文件夾設置爲777,但它似乎仍然無法工作。有沒有人有建議,或看到我接近這個錯誤?

謝謝!

回答

4

未來讀者更新。這裏的根本問題是,你必須完全建立ASIFormDataRequest項目是這樣的:

NSURL *url = [NSURL URLWithString:@"https://www.you.com/cgi-bin/you.cgi?stuff"]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

不要更多的釋放增加,保留,自動釋放,自我,等等等等!就是這樣。

嗯。我不完全確定autorelease是否正確。你必須小心不要在ASIHttpRequest中丟失resquest指針。

設置,放在一邊,這將是值得一試它首先是一個簡單的(非排隊,異步)的要求,使用的正是這種模式已經測試了數十億次:

-(void) sendSomethingAnything 
    { 
    NSURL *url = [NSURL URLWithString:@"https://www.you.com/cgi-bin/you.cgi?stuff"]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

    [request setPostValue:@"fakeIDstring" forKey:@"id"]; 
    [request setPostValue:@"anotherCalue" forKey:@"username"]; 
    // your file here .. 

    [request setDelegate:self]; 
    [request setDidFinishSelector:@selector(postFinished:)]; 
    [request setDidFailSelector:@selector(postFailed:)]; 

    [request startAsynchronous]; 
    } 

就在粘貼網址,你可以立即嘗試。這將消除許多問題,並讓我們更接近問題。在完成的例程中,獲取並顯示像這樣的結果...

NSData *newStringData = [request responseData]; 
NSString *x = [[[NSString alloc] initWithData:newStringData 
        encoding:NSUTF8StringEncoding] autorelease]; 
NSLog(@"result text from server is %@", x); 

最後在你的PHP中。我發現添加一行代碼,EMAILS ME可以從php中指出發生了什麼。或者,使用「牆」命令,並在您使用客戶端時在您的服務器上打開一個shell。只有這樣,才能看到工作中發生了什麼。

(其實我使用老式的Perl,而不是未來的PHP,所以我不能提供有關這一目的的任何代碼:)!)

+0

+1用於在創建請求時刪除無關的alloc + init + autorelease。我已經完美的成功上傳文件到一個像喬的代碼的PHP頁面。 – 2010-12-06 09:24:55

0

我做到這一點,它肯定works.Make要做到這一點 - >#進口 「ASIFormDataRequest.h」

NSURL * URL = [NSURL URLWithString:@ 「XXXXXX」]; ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url]; [ASIFormDataRequest requestWithURL:url];

[request setPostValue:@"fakeIDstring" forKey:@"id"]; 
[request setPostValue:@"anotherCalue" forKey:@"username"]; 
// your file here .. 
[request setFile:@"path/filename.png" forKey:@"uploadedfile"]; 

[request setDelegate:self]; 
[request setDidFinishSelector:@selector(requestDone:)]; 
[request setDidFailSelector:@selector(requestWentWrong:)]; 

[request startAsynchronous]; 

然後在PHP代碼

$ target_path = 「文件/」;

$ target_path = $ target_path。 basename( $ _FILES ['uploadedfile'] ['name']);

if(move_uploaded_file($ _ FILES ['uploadedfile'] ['tmp_name'], $ target_path)){echo「The file」。 basename( $ _FILES ['uploadedfile'] ['name'])。 「 」已上傳「; } else {echo「上傳 文件時出錯,請重試!」; }

請注意,你應該在你的php文件旁邊有個「files」文件夾來處理這個上傳的文件。