https://stackoverflow.com/questions/4591059/download-file-from-ftp-with-progress-totalbytestoreceive-is-always-1

With FTP protocol, WebClient in general does not know total download size. So you commonly get -1 with FTP.

Note that the behavior actually contradicts the .NET documentation, which says for FtpWebResponse.ContentLength (where the value of TotalBytesToReceive comes from):

For requests that use the DownloadFile method, the property is greater than zero if the downloaded file contained data and is zero if it was empty.
But you will easily find out many of questions about this, effectively showing that the behavior is not always as documented. The FtpWebResponse.ContentLength has a meaningful value for GetFileSize method only.

The FtpWebRequest/WebClient makes no explicit attempt to find out a size of the file that it is downloading. All it does is that it tries to look for (xxx bytes). string in 125/150 responses to RETR command. No FTP RFC mandates that the server should include such information. ProFTPD (see data_pasv_open in src/data.c) and vsftpd (see handle_retr in postlogin.c) seem to include this information. Other common FTP servers (IIS, FileZilla) do not do this.

If your server does not provide size information, you have to query for size yourself before download. A complete solution using FtpWebRequest and Task:

private void button1_Click(object sender, EventArgs e)
{
// Run Download on background thread
Task.Run(() => Download());
}

private void Download()
{
try
{
const string url = "ftp://ftp.example.com/remote/path/file.zip";
NetworkCredential credentials = new NetworkCredential("username", "password");

// Query size of the file to be downloaded
WebRequest sizeRequest = WebRequest.Create(url);
sizeRequest.Credentials = credentials;
sizeRequest.Method = WebRequestMethods.Ftp.GetFileSize;
int size = (int)sizeRequest.GetResponse().ContentLength;

progressBar1.Invoke(
(MethodInvoker)(() => progressBar1.Maximum = size));

// Download the file
WebRequest request = WebRequest.Create(url);
request.Credentials = credentials;
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
int position = (int)fileStream.Position;
progressBar1.Invoke(
(MethodInvoker)(() => progressBar1.Value = position));
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
enter image description here

The core download code is based on:
Upload and download a binary file to/from FTP server in C#/.NET

C# show FTP Download/Upload progress的更多相关文章

  1. python ftp download with progressbar

    i am a new one to learn Python. Try to download by FTP. search basic code from baidu. no one tells h ...

  2. [Powershell] FTP Download File

    # Config $today = Get-Date -UFormat "%Y%m%d" $LogFilePath = "d:\ftpLog_$today.txt&quo ...

  3. FTP Download File By Some Order List

    @Echo Off REM -- Define File Filter, i.e. files with extension .RBSet FindStrArgs=/E /C:".asp&q ...

  4. PHP5.4新特性之上传进度支持Upload progress

    在PHP5.4版本当中给我们提供了好用的特性,上传进度的支持,我们可以配合Ajax动态获取SESSION当中的上传进度: 在使用这一特性之前,需要现在php.ini文件当中进行相应的设置:   1 2 ...

  5. PHP上传进度支持(Upload progress in sessions)

    文件上传进度反馈, 这个需求在当前是越来越普遍, 比如大附件邮件. 在PHP5.4以前, 我们可以通过APC提供的功能来实现. 或者使用PECL扩展uploadprogress来实现. 从PHP的角度 ...

  6. pycurl,Python cURL library

    pycurl — A Python interface to the cURL library Pycurl包是一个libcurl的Python接口.pycurl已经成功的在Python2.2到Pyt ...

  7. Pycurl介绍

    pycurl — A Python interface to the cURL library Pycurl包是一个libcurl的Python接口.pycurl已经成功的在Python2.2到Pyt ...

  8. Atitit s2018.5 s5 doc list on com pc.docx  v2

    Atitit s2018.5 s5  doc list on com pc.docx  Acc  112237553.docx Acc Acc  112237553.docx Acc baidu ne ...

  9. Atitit s2018.5 s5 doc list on com pc.docx  Acc 112237553.docx Acc baidu netdisk.docx Acc csdn 18821766710 attilax main num.docx Atiitt put post 工具 开发工具dev tool test.docx Atiitt 腾讯图像分类相册管家.docx

    Atitit s2018.5 s5  doc list on com pc.docx  Acc  112237553.docx Acc baidu netdisk.docx Acc csdn 1882 ...

随机推荐

  1. Loadrunner 脚本开发-利用web_custom_request函数进行接口测试

    脚本开发-利用web_custom_request函数进行接口测试 by:授客 QQ:1033553122 一.POST + JSON格式参数 例: web_custom_request(" ...

  2. plsql 导出查询结果

      点击青色按钮即可 说明: 会将查询到的所有数据导出到指定文件,并不是只导出下面列表显示的几行数据: 也不用点击"获取最后页"那个按钮. 注意: 当你选择导出为excel文件时, ...

  3. nginx www解析失败问题解决

    nginx www解析失败: nginx代理IIS下域名时 xxxx.xxx可以解析 但www.xxxx.xxx解析失败 IIS增加ip解析:配置下127.0.0.1就可以解析了.

  4. java实现文件复制粘贴功能

    java编程思想中讲到了IO流的思想,以前对于java基础总是不够深入,浅尝辄止,如今碰到语句插桩的时候就感到书到用时方恨少啊! 文件的复制涉及到源文件和新文件(无需手动创建),给出源文件的路径和文件 ...

  5. [iOS] UICollectionView初始化滚动到中间的bug

    转载请保留地址wossoneri.com 问题 首先看一下我之前写的demo:link demo是封装了一个控件,直接在MainViewController的viewWillAppear里初始化,并且 ...

  6. bootstrap selectpicker 通过代码指定选中值 BootStrap selectpicker后台动态绑定数据 selectpicker 获取选中option的属性或者值

    bootstrap-select使用 bootstrap selectpicker 通过代码指定选中值 $('#subjectno').selectpicker('val',(row.subjectn ...

  7. webpack学习笔记—优化缓存、合并、懒加载等

    除了前面的webpack基本配置,还可以进一步添加配置,优化合并文件,加快编译速度.下面是生产环境配置文件webpack.production.js,与wenbpack.config.js相比其不需要 ...

  8. LeetCode题解之Reorder List

    1.题目描述 2.题目分析 首先将链表分为两段,然后将后面的一段反转,再合并两个链表. 3.代码 void reorderList(ListNode* head) { if (head == null ...

  9. MySQL-死锁查询

    1.查询是否锁表 show OPEN TABLES where In_use > 0; 查询到相对应的进程 === 然后 kill    id 2.查询进程 show processlist 补 ...

  10. [Spark Core] Spark 实现气温统计

    0. 说明 聚合气温数据,聚合出 MAX . MIN . AVG 1. Spark Shell 实现 1.1 MAX 分步实现 # 加载文档 val rdd1 = sc.textFile(" ...