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 ...
随机推荐
- tilestache + mbutil应用
1. 安装pip. 我们同样需要在Python的官网上去下载,下载地址是: https://pypi.python.org/pypi/pip#downloads 2. 解压. 解压pip-9.0.1. ...
- 葡萄城报表介绍:矩表(Tablix)
一.矩表(Tablix)定义 Tablix 即表(Table)+矩阵(Matrix)+列表(List),它是报表中展示数据的一种工具,由表.矩阵.列表这三个具有相似性的报表项组成. 二.矩表(Tabl ...
- SQLServer 中的身份验证及登录问题
SQLServer 中的身份验证及登录问题 by:授客 QQ:1033553122 身份验证 SQL Server 支持两种身份验证模式,即Windows 身份验证模式和混合模式. Windows 身 ...
- android-studio开发NDK错误记录:bash: ../../build/intermediates/classes/debug: is a directory
按照网上很多已有的教程,在用javah生成c的头文件时候报错: Error: no classes specified bash: ../../build/intermediates/classes/ ...
- 【转】使用windeployqt.exe进行依赖查找打包
原文:https://blog.csdn.net/u011822862/article/details/52166940 Qt 官方开发环境使用的动态链接库方式,在发布生成的可执行程序时,需要复制可执 ...
- android 解决连接电视机顶盒失败的方法
今天在开发过程中,需要连接海美迪的电视盒子,这个盒子是基于android6.0的版本,之前连接其它电视盒子都正常,当输入 adb -s xxxx shell后,盒子连接失败,日志如下: error: ...
- 犀牛Rhino教程合集37部
犀牛Rhino教程合集37部 教程说明:英文视频教程,部分有中文字幕,大部分有工程文件 教程格式:Flv.MP4格式,大部分高清,确保能看清软件上的文字 发货方式:百度网盘下载链接(教程较多,可转存到 ...
- Git 命令快速浏览
Git 命令快速浏览 创建 Git 可管理的仓库 git init 查看当前仓库的状态 git status 添加到仓库,实际上是添加到暂存区 git add [-A | --all] git add ...
- mysql中删除同一行会经常出现死锁?太可怕了
之前有一个同事问到我,为什么多个线程同时去做删除同一行数据的操作,老是报死锁,在线上已经出现好多次了,我问了他几个问题: 1. 是不是在一个事务中做了好几件事情? 答:不是,只做一个删除 ...
- Oracle EBS OPM update material txn
--update_material_txn --created by jenrry DECLARE p_mmti_rec mtl_transactions_interface%ROWTYPE; p_m ...