2012-01-06 86 views
1

我有一個Windows窗體應用程序,其中我使用後臺工作器來上傳文件。在成功上傳209文件後,它給出了只有7.8kb大小的文件的錯誤While Processing Img1.jpg Unable to write data to the transport connection. An existing connection was forcibly closed by the remote hostFtp Upload給出異常無法將數據寫入傳輸連接。一個現有的連接被遠程主機強行關閉了

string uri1; 

ftpInfoUpload = LoadHostedSiteData(hs); 
ftpInfoUpload[5] = imgRow["Filename"].ToString(); 

uri1 = String.Format("ftp://{0}/{1}/images/{2}", ftpInfoUpload[1], ftpInfoUpload[2], ftpInfoUpload[5]); 

requestUpload = (FtpWebRequest)WebRequest.Create(uri1); 
requestUpload.UsePassive = false; 
requestUpload.UseBinary = true; 
requestUpload.Method = WebRequestMethods.Ftp.UploadFile; 
requestUpload.Credentials = new NetworkCredential(ftpInfoUpload[3], ftpInfoUpload[4]); 


requestUpload.ContentLength = memStream.Length; 
byte[] buff = new byte[bufferSize]; 
int contentLen; 

// Stream to which the file to be upload is written 
Stream strm = requestUpload.GetRequestStream(); 
memStream.Seek(0, SeekOrigin.Begin); 
contentLen = memStream.Read(buff, 0, bufferSize); 
          // Till Stream content ends 
while (contentLen > 0) 
{ 
    // Write Content from the file stream to the FTP Upload Stream 
    strm.Write(buff, 0, contentLen); 
    contentLen = memStream.Read(buff, 0, bufferSize); 
} 

//Close the file stream and the Request Stream 
strm.Close(); 
strm.Dispose(); 
ftpStream.Close(); 
memStream.Close(); 
//responseUpload.Close(); 
responseDownload.Close(); 

想法發生了什麼?

+0

您可能超過了FTP服務器上的配額,因此中止了連接。聯繫服務器管理員以獲得支持。 – 2012-01-06 17:06:05

+0

@HansPassant配額文件數量?服務器有一個用戶可以傳輸的文件數量的配額? – PUG 2012-01-06 17:15:31

+0

可能不是。相反,你所造成的交通總量的配額(取決於你上傳的文件的大小)或者只是一個簡單的連接重置 – yas4891 2012-01-06 18:18:55

回答

1

我已經設置了ftprequest.KeepAlive=true &設置ftprequest.ConnectionGroupName = "Some Value",以便底層代碼不必新建具有相同FTP服務器的連接。我發現這個解決方案here。我也發現this有幫助。每次傳輸可能導致異常的文件時,請確保不要創建新的NetworkCredential對象。我已經測試了兩次傳輸300個文件的代碼,並且似乎能夠完美快速地工作。設置KeepAlive=false可能會導致傳輸速度變慢

相關問題