之前使用下载也都没管中间过程,最近有时间就研究了一下,FTP在获取文件大小时发现在调用e.TotalBytesToReceive时一直是-1,,而e.ProgressPercentage一直是0..只在最后一瞬间变化
看了许多文章也没找到解决方法,后面在一个问题的评论区看到有人说ftp是不像http那样返回的,要提前自己获取.我也不太明白
这是我目前测试提前读取文件大小最合适的位置
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
FtpWebRequest FTPWbReq = WebRequest.Create(url) as FtpWebRequest;
FTPWbReq.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse FTPWebRes = FTPWbReq.GetResponse() as FtpWebResponse;
length = FTPWebRes.ContentLength;
FTPWebRes.Close();
this.BeginInvoke((MethodInvoker)delegate {
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = length;//double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
label2.Text = "Downloaded" + e.BytesReceived + " of" + totalBytes;// e.TotalBytesToReceive;
progressBar1.Value =int.Parse(Math.Truncate(percentage).ToString());
this.label1.Text = "正在下载...";
label3.Text = percentage.ToString()+"%";
});