[c#]WebClient异步下载文件并显示进度
摘要
在项目开发中经常会用到下载文件,这里使用winform实现了一个带进度条的例子。
一个例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace Wolfy.DownLoad
{
public partial class MainFrm : Form
{
private string _saveDir;
public MainFrm()
{
InitializeComponent();
_saveDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");
} private void MainFrm_Load(object sender, EventArgs e)
{ if (!Directory.Exists(_saveDir))
{
Directory.CreateDirectory(_saveDir);
} } private void btnDownLoad_Click(object sender, EventArgs e)
{
string imageUrl = "http://image.3761.com/nvxing/2016022921/2016022921382152113.jpg"; string fileExt = Path.GetExtension(imageUrl);
string fileNewName = Guid.NewGuid() + fileExt;
bool isDownLoad = false;
string filePath = Path.Combine(_saveDir, fileNewName);
if (File.Exists(filePath))
{
isDownLoad = true;
}
var file = new FileMessage
{
FileName = fileNewName,
RelativeUrl = "nvxing/2016022921/2016022921382152113.jpg",
Url = imageUrl,
IsDownLoad = isDownLoad,
SavePath = filePath
}; if (!file.IsDownLoad)
{ string fileDirPath = Path.GetDirectoryName(file.SavePath);
if (!Directory.Exists(fileDirPath))
{
Directory.CreateDirectory(fileDirPath);
}
try
{
WebClient client = new WebClient();
client.DownloadFileCompleted += client_DownloadFileCompleted;
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileAsync(new Uri(file.Url), file.SavePath, file.FileName);
}
catch
{ } }
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.UserState != null)
{
this.lblMessage.Text = e.UserState.ToString() + ",下载完成";
}
} void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.proBarDownLoad.Minimum = ;
this.proBarDownLoad.Maximum = (int)e.TotalBytesToReceive;
this.proBarDownLoad.Value = (int)e.BytesReceived;
this.lblPercent.Text = e.ProgressPercentage + "%";
}
}
}
测试


[c#]WebClient异步下载文件并显示进度的更多相关文章
- 实现在 .net 中使用 HttpClient 下载文件时显示进度
在 .net framework 中,要实现下载文件并显示进度的话,最简单的做法是使用 WebClient 类.订阅 DownloadProgressChanged 事件就行了. 但是很可惜,WebC ...
- WebClient异步下载文件
namespace ConsoleAppSyncDownload{ class Program { static void Main(string[] args) { ...
- C# Winform下载文件并显示进度条
private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...
- Winform下载文件并显示进度条
本来是要研究怎样判断下载完成,结果找到这个方法,可以在这个方法完成之后提示下载完成. 代码如下: using System; using System.Collections.Generic; usi ...
- 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具
转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...
- 通过HttpUrlConnection下载文件并显示进度条
实现效果: 核心下载块: int count = 0; URL url = new URL("http://hezuo.downxunlei.com/xunlei_hezuo/thunder ...
- WebClient.DownloadFile(线程机制,异步下载文件)
线程机制(避免卡屏),异步下载文件. 我做网站的监控,WebClient.DownloadFile这个方法是我经常用到的,必要的时候肯定是要从网上下载些什么(WebRequest 也可以下载网络文件, ...
- 一个简单的利用 WebClient 异步下载的示例(二)
继上一篇 一个简单的利用 WebClient 异步下载的示例(一) 后,我想把核心的处理提取出来,成 SkyWebClient,如下: 1. SkyWebClient 该构造函数中 downloadC ...
- AsyncTask用法解析-下载文件动态更新进度条
1. 泛型 AysncTask<Params, Progress, Result> Params:启动任务时传入的参数,通过调用asyncTask.execute(param)方法传入. ...
随机推荐
- HDU 1068 Girls and Boys(最大独立集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 题目大意:有n个人,一些人认识另外一些人,选取一个集合,使得集合里的每个人都互相不认识,求该集合 ...
- shell 中>/dev/null 2>&1含义
shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 分解这个组合:“>/dev/null 2>&1” 为五 ...
- python 删除前3天的文件
一.需求分析 1. 删除前3天的文件 2.如果目录为空,也一并删除掉 如果使用shell脚本,一条命令就搞定了.干啥还要用python? 1. 因为需要记录一些日志,使用shell不好实现 2. 作为 ...
- PHP性能调优,PHP慢日志---善用php-fpm的慢执行日志slow log,分析php性能问题
众所周知,MySQL有slow query log,根据慢查询日志,我们可以知道那些sql语句有性能问题.作为mysql的好搭档,php也有这样的功能.如果你使用php-fpm来管理php的话,你可以 ...
- explicit 显示的类型转换运算符
C++提供了关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生.声明为explicit的构造函数不能在隐式转换中使用. 调用构造函数可以分为显示调用和隐式调用,当用赋值初 ...
- 【51nod】1164 最高的奖励 V2
题解 一道比较神奇的二分图匹配 既然有n个元素,那么能匹配n个位置,我们把这n个位置找出来,是每个区间从左端点开始找到一个没有被匹配到的位置作为该点(我们忽略右端点) 然后我们从价值大到小,然后从左端 ...
- 如何解决谷歌Chrome浏览器空白页的问题
如何解决谷歌Chrome浏览器空白页的问题 谷歌Chrome浏览器突然不打开任何网页,无论是任何站点(如http://www.baidu.com), 还是Chrome浏览器的设置页面(chrome ...
- express增加swagger功能
参考地址:https://blog.csdn.net/freeboy1234/article/details/79289486 下载swagger ui库 地址是:https://github.com ...
- PHP接入微信H5支付
开发前配置 进行代码接入前,需在微信后台填写授权回调域名,此域名必须经过ICP备案 开发主要流程 用户下单时选择微信支付 商户进行业务逻辑处理并调用微信统一下单接口,微信H5交易类型为:trade_t ...
- Bzoj1018/洛谷P4246 [SHOI2008]堵塞的交通(线段树分治+并查集)
题面 Bzoj 洛谷 题解 考虑用并查集维护图的连通性,接着用线段树分治对每个修改进行分治. 具体来说,就是用一个时间轴表示图的状态,用线段树维护,对于一条边,我们判断如果他的存在时间正好在这个区间内 ...