1

我有一个 Windows 窗体应用程序,我在其中使用后台工作程序来 ftp 上传文件。成功上传209文件后,它给出的文件错误只有 7.8kb 的大小While Processing Img1.jpg Unable to write data to the transport connection. An existing connection was forcibly closed by the remote host

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();

和想法发生了什么?

4

1 回答 1

1

我已经 set ftprequest.KeepAlive=true& set ftprequest.ConnectionGroupName = "Some Value",因此底层代码不必新创建具有相同 ftp 服务器的连接。我在这里找到了这个解决方案。我也发现很有帮助。还要确保不要在NetworkCredential每次传输可能导致异常的文件时创建新对象。我已经测试了我的代码两次传输 300 个文件,并且似乎完美而快速地工作。设置KeepAlive=false会使传输变慢

于 2012-02-03T20:33:55.027 回答