1

バックグラウンドワーカーを使用してアップロードファイルをFTPで転送する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 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

基礎となるコードが同じftpサーバーを持つ接続を新たに作成する必要がないようにftprequest.KeepAlive=true設定して設定しました。私はここftprequest.ConnectionGroupName = "Some Value"でこの解決策を見つけました。これも役に立ちました。また、例外を引き起こす可能性のあるファイルを転送するたびに、新しいオブジェクトを作成しないように注意してください。300ファイルを転送するコードを2回テストしましたが、完全かつ迅速に機能するようです。設定すると転送が遅くなる可能性がありますNetworkCredentialKeepAlive=false

于 2012-02-03T20:33:55.027 に答える