2015-04-15 40 views
0

我使用此代碼上傳文件到FTP服務器:C# - 在FTP上傳文件不能正常工作

String source = @"E:\\file\e1.txt"; 
String ftpurl = @"ftp://ftp.myftpserver.pl"; 
String ftpusername = @"[email protected]"; 
String ftppassword = @"mypassword"; 

string filename = Path.GetFileName(source); 
string ftpfullpath = ftpurl + "/" + filename; 
Debug.WriteLine("Uploading..."); 

var fs = File.OpenRead(source); 

FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); 
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword); 
ftp.Timeout = -1; 
ftp.KeepAlive = true; 
ftp.UseBinary = true; 
ftp.Method = WebRequestMethods.Ftp.UploadFile; 

Stream ftpstream = ftp.GetRequestStream(); 
fs.CopyTo(ftpstream); 
ftpstream.Close(); 


Debug.WriteLine("Upload complete."); 

文件已退出大 - 約50萬桶。所以,上傳文件後,appliaction掛起。這是在調試窗口

Uploading... //immediately after executing the code 
The thread 0xd50 has exited with code 0 (0x0). //after some time 

輸出它從來沒有打印:

Upload complete. 

注意,該線程退出後,上傳仍在進行中,它不會被取消。我已經注意到這個問題,當我試圖在循環中從List發送多個文件時 - 應用程序在上載第一個文件後掛起。 有人可以幫我嗎?

//編輯 出於測試目的,我試圖在for循環中上傳更小的文件。它似乎工作即使

The thread XXX has exited with code 0 (0x0). 

所以這是因爲尺寸的,但我的服務器將接受更大的文件,我對FTP空間。有任何想法嗎?

+0

把它放在try catch並讀取異常 – Tuco

+0

沒有例外。 –

+0

嘗試上傳更小尺寸的較小文件? – Jonesopolis

回答

1

很有可能您在控制通道上有超時。數據通道很忙,因此很快樂......但是,您在代碼中設置的任何超時或keepalive將不幸在此處不適用。深入檢查您的FTP日誌應該顯示文件正在上傳,然後會話意外關閉。 檢查防火牆和負載平衡器,如果你有一個(F5的默認值對我來說完全一樣的話......)