从FTP下载具有进度的文件 - TotalBytesToReceive总是-1? [英] Download file from FTP with Progress - TotalBytesToReceive is always -1?
问题描述
文件正在下载,并且ProgressChanged事件正在调用,除了事件参数TotalBytesToReceive始终为-1。 TotalBytes增加了,但是我无法计算没有总数的百分比。
我想我可以通过其他ftp命令找到文件大小,但是我想知道为什么这样做不能实现,
我的代码:
FTPClient request = new FTPClient ();
request.Credentials = credentials;
request.DownloadProgressChanged + = new DownloadProgressChangedEventHandler(request_DownloadProgressChanged);
//request.DownloadDataCompleted + = new DownloadDataCompletedEventHandler(request_DownloadDataCompleted);
request.DownloadDataAsync(new Uri(folder + file));
while(request.IsBusy);
....
static void request_DownloadProgressChanged(object sender,DownloadProgressChangedEventArgs e)
{
if(e.TotalBytesToReceive == -1)
{
l.reportProgress( - 1,FormatBytes(e.BytesReceived)+out of?);
}
else
{
l.reportProgress(e.ProgressPercentage,Downloaded+ FormatBytes(e.BytesReceived)+out of+ FormatBytes(e.TotalBytesToReceive)+ (+ e.ProgressPercentage +%));
}
}
....
class FTPClient:WebClient
{
保护覆盖WebRequest GetWebRequest(System.Uri地址)
{
FtpWebRequest req =(FtpWebRequest)base.GetWebRequest(address);
req.UsePassive = false;
return req;
$ / code $ / pre
$ b $ p感谢。
解决方案所以我有同样的问题。我通过首先检索文件大小来解决它。
//获取用于与服务器通信的对象。
FtpWebRequest请求=(FtpWebRequest)WebRequest.Create(URL);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Credentials = networkCredential;
FtpWebResponse response =(FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
bytes_total = response.ContentLength; //这是一个int成员变量,用于以后存储
Console.WriteLine(Fetch Complete,ContentLength {0},response.ContentLength);
response.Close();
webClient = new MyWebClient();
webClient.Credentials = networkCredential; ;
webClient.DownloadDataCompleted + = new DownloadDataCompletedEventHandler(FTPDownloadCompleted);
webClient.DownloadProgressChanged + = new DownloadProgressChangedEventHandler(FTPDownloadProgressChanged);
webClient.DownloadDataAsync(new Uri(URL));
然后在回调中进行数学运算。
< pre $ private void FTPDownloadProgressChanged(object sender,DownloadProgressChangedEventArgs e)
{
progressBar.Value =(int)(((float)e.BytesReceived /(float)bytes_total )* 100.0);
}
I am trying to download a file from an FTP server with a progress bar.
The file is downloading, and the ProgressChanged event is calling, except in the event args TotalBytesToReceive is always -1. TotalBytes increases, but I am unable to calculate the percentage without the total.
I imagine I could find the file size through other ftp commands, but I wonder why this doesn't work?
My code:
FTPClient request = new FTPClient();
request.Credentials = credentials;
request.DownloadProgressChanged += new DownloadProgressChangedEventHandler(request_DownloadProgressChanged);
//request.DownloadDataCompleted += new DownloadDataCompletedEventHandler(request_DownloadDataCompleted);
request.DownloadDataAsync(new Uri(folder + file));
while (request.IsBusy) ;
....
static void request_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (e.TotalBytesToReceive == -1)
{
l.reportProgress(-1, FormatBytes(e.BytesReceived) + " out of ?" );
}
else
{
l.reportProgress(e.ProgressPercentage, "Downloaded " + FormatBytes(e.BytesReceived) + " out of " + FormatBytes(e.TotalBytesToReceive) + " (" + e.ProgressPercentage + "%)");
}
}
....
class FTPClient : WebClient
{
protected override WebRequest GetWebRequest(System.Uri address)
{
FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
req.UsePassive = false;
return req;
}
}
Thanks.
So I had the same issue. I got around it by retrieving the file size first.
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("URL");
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Credentials = networkCredential;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
bytes_total = response.ContentLength; //this is an int member variable stored for later
Console.WriteLine("Fetch Complete, ContentLength {0}", response.ContentLength);
response.Close();
webClient = new MyWebClient();
webClient.Credentials = networkCredential; ;
webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(FTPDownloadCompleted);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(FTPDownloadProgressChanged);
webClient.DownloadDataAsync(new Uri("URL"));
Then do the math in the callback.
private void FTPDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = (int)(((float)e.BytesReceived / (float)bytes_total) * 100.0);
}
这篇关于从FTP下载具有进度的文件 - TotalBytesToReceive总是-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!