1
大文件
我正在做的FTP客戶端,並試圖上傳文件到服務器(〜300 MB),但我得到以下時,幾乎100 MB的文件被轉移錯誤:例外通過FTP上傳
The underlying connection was closed: An unexpected error occurred on a receive.
這裏是我的代碼:
private void UploadFile(string filepath, string filename)
{
try
{
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://" + server + "/" + filename);
//ftp.KeepAlive = false;
//ftp.Timeout = 1000000;
//ftp.UsePassive = true;
//ftp.ReadWriteTimeout = 100000;
Path.GetFileName(filepath);
ftp.Credentials = new NetworkCredential(username, password);
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream stream = File.OpenRead(filepath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
Stream requestStream = ftp.GetRequestStream();
//requestStream.ReadTimeout = 1000000;
//requestStream.WriteTimeout = 1000000;
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
response.Close();
}
catch (Exception ex) { CreateRunLogFile(ex.Message); }
CreateRunLogFile("Uploading of file " + filepath + " ended.");
}
我嘗試使用,
ftp.KeepAlive = false;
ftp.Timeout = 1000000;
ftp.UsePassive = true;
但它並沒有幫助。
該文件是否仍在傳輸?這可能是控制通道的超時,沒有任何實際的錯誤。另外,您是否嘗試過使用ftp客戶端(如FileZilla)上傳相同的文件?你看到什麼行爲? – JamieMeyer
@JamieMeyer,不,當程序拋出exe文件傳輸結束時。關於控制通道,我不明白。我試圖用FileZilla和WinSCP上傳,文件加載成功。還有一件事,我注意到,在上傳100秒之後拋出了這個豁免。 – vasa911
您建立的超時值以毫秒爲單位,這可能解釋發生了什麼。此外,這似乎也是默認設置,與記錄的默認設置相反。嘗試將其設置爲-1並再試一次。這可能有所幫助:http://www.sidesofmarch.com/index.php/archive/2012/04/06/damn-the-documentation-ftpwebrequest-timeout-default-value-is-not-infinite/ – JamieMeyer