NET Framework 4.6
我的代码中出现了一个奇怪而又困难的错误。当我上传一个ZIP文件时; 150MB(慢速互联网,需要15分钟才能上传),有时任务没有100%完成。它永远不会到达控制台的最后一行,并在while循环时挂在进度条上。
FTP服务器上的文件已完成且未损坏。那么,为什么它会挂在某些文件上(10次中的5次)?
尝试了其他旧帖子中的一些帖子,说这可能是请求配置。在while循环之后也尝试关闭ftpStream / fileStream。但这不会有任何效果。
希望有人可以帮助我。这是任务:
Task TaskA = Task.Run(async () =>
{
// Get filesize
double len = new FileInfo(zipPathFull).Length;
string resultSize = ConvertToReadable(len);
ThreadSafeUpdateStatus(String.Format("Zip bestand aangemaakt, uploaden {0}...", resultSize));
// Verstuur ZIP over FTP
Console.WriteLine("Starting FTP commands...");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://DOMAIN.nl/domains/DOMAIN.nl/public_html/transfer/zips/" + zipfilename + ".zip");
request.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
request.Timeout = -1;
request.KeepAlive = true;
request.UseBinary = true;
request.UsePassive = true;
request.ServicePoint.ConnectionLimit = 1000;
request.Method = WebRequestMethods.Ftp.UploadFile;
Console.WriteLine("Uploading over FTP...");
using (Stream fileStream = File.OpenRead(zipPathFull))
using (Stream ftpStream = request.GetRequestStream())
{
byte[] buffer = new byte[10240];
int read;
while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
Console.WriteLine("Buffer status: " + (int)fileStream.Position);
ftpStream.Write(buffer, 0, read);
var percent = 100m * ((decimal)fileStream.Position / fileStream.Length);
double doublepercentage = (double)percent;
ThreadSafeUpdateProgress(doublepercentage);
string CurrentresultSize = ConvertToReadable((double)fileStream.Position);
ThreadSafeUpdateStatus(String.Format("Zip bestand aangemaakt, uploaden {0}/{1}...", CurrentresultSize, resultSize));
}
ftpStream.Close();
fileStream.Close();
}
request.Abort();
Console.WriteLine("FTP Done");
.....
});