[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)方法传入. ...
随机推荐
- C++两个类相互包含引用的问题
在构造自己的类时,有可能会碰到两个类之间的相互引用问题,例如:定义了类A类B,A中使用了B定义的类型,B中也使用了A定义的类型 class A { B b; } class B { A* a; } 请 ...
- nginx重写链接
location ~ /send.redir { if ( $query_string ~ “url=(?<path>.+)” ) {rewrite ^.* htt ...
- 阿里百川码力APP监控 来了!
阿里百川码力APP监控 来了!这个APP监控 和手淘一起成长历经千锤百炼 走过千BUG万坑如今百川起产品 为了让你的APP更好 用户更爽! 在移动互联网时代,一款应用是否成功,用户体验是一个关键 ...
- 2016-2017-2 20155309南皓芯java第四周学习总结
教材内容总结 这次我们学习的还是两章的内容,学习任务量跟上次比的话大体上来讲是差不多的. 继承与多态 继承 继承也符合DRY(Don't Repeat Yourself)原则 Role role1 = ...
- 快速php日志,写内容到文件,把日志写到log文件
php 写内容到文件,把日志写到log文件 //记录日志:要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个.log.txt位置在项目的根目录下. $file = 'log.txt' ...
- centos7上使用locate命令
https://blog.csdn.net/yqh19880321/article/details/72426879
- Asp.net mvc 实时生成缩率图到硬盘
之前对于缩率图的处理是在图片上传到服务器之后,同步生成两张不同尺寸的缩率供前端调用,刚开始还能满足需求,慢慢的随着前端展示的多样化,缩率图已不能前端展示的需求,所以考虑做一个实时生成图片缩率图服务. ...
- day9--paramiko模块
志不坚者智不达 paramiko:在Linux链接其他机器,每台Linux机器都有一个SSHClient:Python自己也写了一个SSHClient,那么Python写paramiko创建SSHCl ...
- web中的相对路径与绝对路径
1.什么叫绝对路径 相对于WEB应用的跟路径的路径,即任何路径都必须带上contentPath. 2.javaEE中的/代表什么 代表WEB应用的跟路径(需交由Servlet容器处理) 请求转发时. ...
- 百度地图API--Key的获得
[开年后花了半个月的时间学习了百度地图API开发,准备投入项目中,学习的过程中写了一些简单的总结,在部门内部做了一个简单的分享培训,这里希望将自己的仅有的一点点关于百度地图API的收获分享给社区,整个 ...