我上傳我的文件使用下面的代碼,它工作正常,但一段時間它引發此錯誤「底層連接已關閉:服務器提交協議違規」,並停止進程,當我再次運行它上傳文件與解決任何問題。底層連接已關閉:服務器違反協議。 FTP
我注意到的一件事,如果我運行這個過程上傳多個文件,然後我有時得到這個錯誤,如果我做少於5個文件,那麼它工作正常,任何想法在哪裏,我有什麼看它。
我在谷歌搜索沒有發現這個固體的解決方案,甚至有一個msdn博客說它在FTPwebrequest中的錯誤,但不知道。
環境:C#4.0,IIS FTP服務器。
由於提前
FileInfo fileInf = new FileInfo(filename);
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create
(new Uri(path + fileInf.Name));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(user, pwd);
// By default KeepAlive is true, where the control connection
// is not closed after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
reqFTP.Timeout = -1;
reqFTP.UsePassive = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 4096;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file
// to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload
// Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
回答
reqFTP.KeepAlive = FALSE;到「真」來擺脫我的錯誤。
更改此行以擺脫錯誤「reqFTP.KeepAlive = True;」 – Usher 2012-03-27 23:39:51