我正在制作一个 FTP 客户端并尝试将文件上传到服务器(~300 MB),但是当传输了近 100 MB 的文件时出现以下错误:
The underlying connection was closed: An unexpected error occurred on a receive.
这是我的代码:
private void UploadFile(string filepath, string filename)
{
try
{
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://" + server + "/" + filename);
//ftp.KeepAlive = false;
//ftp.Timeout = 1000000;
//ftp.UsePassive = true;
//ftp.ReadWriteTimeout = 100000;
Path.GetFileName(filepath);
ftp.Credentials = new NetworkCredential(username, password);
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream stream = File.OpenRead(filepath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
Stream requestStream = ftp.GetRequestStream();
//requestStream.ReadTimeout = 1000000;
//requestStream.WriteTimeout = 1000000;
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
response.Close();
}
catch (Exception ex) { CreateRunLogFile(ex.Message); }
CreateRunLogFile("Uploading of file " + filepath + " ended.");
}
我尝试使用,
ftp.KeepAlive = false;
ftp.Timeout = 1000000;
ftp.UsePassive = true;
但这没有帮助。