Ftp 上传出现异常无法将数据写入传输连接。现有连接被远程主机强制关闭
我有一个 Windows 窗体应用程序,其中我使用后台工作者来 ftp 上传文件。成功上传 209
文件后,文件大小仅为 7.8kb,处理 Img1.jpg 时无法将数据写入传输连接。现有连接被远程主机强制关闭
。
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();
和想法发生了什么?
I have a windows form application in which i am using a background worker to ftp upload files. After uploading 209
files successfully it gave error on file which only had size of 7.8kb that 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();
And ideas whats happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经设置了
ftprequest.KeepAlive=true
&设置 ftprequest.ConnectionGroupName = "Some Value",这样底层代码就不必重新创建具有相同 ftp 服务器的连接。我在此处找到了此解决方案。我还发现这很有帮助。另请确保每次传输可能导致异常的文件时不要创建新的 NetworkCredential 对象。我已经测试了我的代码两次,传输了 300 个文件,看起来工作完美且快速。设置KeepAlive=false
可能会使传输变慢I have set
ftprequest.KeepAlive=true
& setftprequest.ConnectionGroupName = "Some Value"
, so that underlying code does not have to new create connection which have the same ftp server. I found this solution here. I also found this helpful. Also make sure not to create a newNetworkCredential
object everytime you transfer a file that can cause exception. I have tested my code twice transferring 300 files and seems to work perfectly and quick. SettingKeepAlive=false
can make transfers slow