[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)方法传入. ...
随机推荐
- DOS命令大全(一)
#1 一: net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" ...
- Java 容器的打印
Java容器类库中的两种主要类型,它们的区别在于容器中每个"槽"保存的元素个数 Clollection容器只能在保存一个元素,此类容器包括: List,它以特定顺序保存一组元素 S ...
- linux下nc提交web报文问题
1.用wireshark截取访问百度首页拿到的请求数据包: GET /index.php?tn=newbdie_bd_dg&bar= HTTP/1.1 Host: www.baidu.com ...
- babel使用中不想使用 严格模式 如何去除?
使用babel进行es6转es5时,默认转化之后是严格模式,有些时候我们想去除严格模式. 解决方法如下 安装 babel-plugin-transform-remove-strict-mode 依赖 ...
- bzoj 1233
先要了解一个结论,在多种可行的堆叠方案中,至少有一种能使层数最高的方案同时使得底边最短.即底边最短的,层数一定最高. dp[ i ] = min(sum[j - 1] - sum[i - 1]) j ...
- bzoj 1295 1295: [SCOI2009]最长距离
思路:对于每个点出发bfs做一次dp, dp[ i ][ j ][ k ] 表示从枚举的该点能不能经过k个障碍物到达(i , j). #include<bits/stdc++.h> #de ...
- HDU - 1716 排列2 水题
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- CSS基础-DAY1
CSS 概述CSS 指层叠样式表 (Cascading Style Sheets),样式定义了如何显示 HTML文件中的标签元素,CSS是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标 ...
- python的可变与不可变数据类型
<python的可变与不可变数据类型> python与C/C++不一样,它的变量使用有自己的特点,当初学python的时候,一定要记住“一切皆为对象,一切皆为对象的引用”这句话,其 ...
- C# NPOCO 轻量级ORM框架(进阶)
继续翻译NPOCO wiki. 这篇将home上 下面的几个页面翻译. wiki地址:https://github.com/schotime/NPoco/wiki 上一篇: http://www.cn ...