C# show FTP Download/Upload progress
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的更多相关文章
- 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 ...
- [Powershell] FTP Download File
# Config $today = Get-Date -UFormat "%Y%m%d" $LogFilePath = "d:\ftpLog_$today.txt&quo ...
- FTP Download File By Some Order List
@Echo Off REM -- Define File Filter, i.e. files with extension .RBSet FindStrArgs=/E /C:".asp&q ...
- PHP5.4新特性之上传进度支持Upload progress
在PHP5.4版本当中给我们提供了好用的特性,上传进度的支持,我们可以配合Ajax动态获取SESSION当中的上传进度: 在使用这一特性之前,需要现在php.ini文件当中进行相应的设置: 1 2 ...
- PHP上传进度支持(Upload progress in sessions)
文件上传进度反馈, 这个需求在当前是越来越普遍, 比如大附件邮件. 在PHP5.4以前, 我们可以通过APC提供的功能来实现. 或者使用PECL扩展uploadprogress来实现. 从PHP的角度 ...
- pycurl,Python cURL library
pycurl — A Python interface to the cURL library Pycurl包是一个libcurl的Python接口.pycurl已经成功的在Python2.2到Pyt ...
- Pycurl介绍
pycurl — A Python interface to the cURL library Pycurl包是一个libcurl的Python接口.pycurl已经成功的在Python2.2到Pyt ...
- 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 ...
- 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 ...
随机推荐
- OSGI企业应用开发(十一)Bundle资源获取途径
使用OSGI模块化标准构建Java EE项目,其中比较繁琐的一个方面就是Bundle资源的获取,因为很多开源框架官方都没有发布Bundle版本的Jar文件,这也是使用OSGI开发企业应用首先要解决的问 ...
- 前端开发笔记(1)html基础
HTML介绍 HTML是HyperTextMarkupLanguage超文本标记语言的缩写 HTML是标记语意的语言 编辑器 任何纯文本编辑器都能够编辑html,比如记事本,editplus,note ...
- [基础知识]在PeopleSoft中SMTP设置不生效如何查找问题
在PeopleSoft中如果配置了工作流邮件或者标准页面的通知,都是可以发送出邮件的,这些邮件都是由SMTP服务器发送.SMTP需要在APP服务器和PRCS服务器中配置. 如果无法从PeopleSof ...
- SD从零开始33-37
[原创]SD从零开始33 Billing简介 Billing在SD流程链中的集成: Billing document表征SD流程链中的最后功能: Billing document在R/3系统的不同区域 ...
- Flutter dart:convert
引用 mport 'dart:convert'; JSON 解码(JSON String->Object) // NOTE: Be sure to use double quotes (&quo ...
- Java计算大整数
import java.util.*; import java.math.*; //BigInteger类型在这个包里 public class Gcc_test { public static vo ...
- python基础学习4----元组
元组又叫只读列表,不可以修改其内容 1.创建元组 tuple1=(1,3,2,4,5) tuple2=()#空元组 tuple3=('a',) #元组中只有一个元素时要在元素后面加上逗号,否则会被当成 ...
- Javaweb学习(三):Servlet程序
好了,既然开发环境已经配置好了.那么我们首先要搞定得便是servlet了,至于为什么不先去研究jsp,这是因为jsp与servlet本就是一体两面,jsp其本身经过编译.载入.转化等步骤最终会成为se ...
- sql行列转换PIVOT与unPIVOT
基本语法 select * from Mould pivot ( count(ID)for ProductTypeCode in ( [FC], [RCU], [RCD] )) as PVT; wit ...
- Learn Algorithms With Javascript - 基于 Js 进行算法学习
基于 javascript 学习并实现常用的经典算法,欢迎对算法和数学感兴趣的 Js 开发者参与,一起学习共同进步. 算法实现 排序 插入排序 sort/lib/insertion-sort.js 希 ...