[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)方法传入. ...
随机推荐
- python之uinttest单元测试框架
unittest,是python中针对单元测试的一个测试框架 相当于python版的junit 简单举个例子: 如图,使用时,测试类需要继承单元测试TestCase这个类 必须要有setUp()和te ...
- 云计算IaaS浅谈
(本篇文章仅仅是整理文档资料时,发现的一篇课程报告,感觉还挺有参考意义的) 最近几年云计算一直是IT业的热点,一股炽热的云计算浪潮席卷了世界,全世界都在讲云计算,都在搞云计算.虽然最初是由谷歌公司提出 ...
- Postman接口&压力测试
Postman接口与压力测试实例 Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件.它提供功能强大的 Web API & HTTP 请求调试. 1.环境变量和全局 ...
- Linux学习笔记:vi常用命令
在Linux系统中常用vi命令进行文本编辑. vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器.Linux中的vi编辑器叫vim,它是vi的增强版(vi Improved), ...
- CF 576A 猜数
A给出一个数x,B每次猜一个y,A回答B,x是否可以被y整除,求出要猜的最小次数和需要猜的数. 枚举每个素数p,可以知道如果p^k<=n,则p^k一定需要选 Sample test(s)inpu ...
- Kafka(一)Kafka的简介与架构
一.简介 1.1 概述 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/ng ...
- 集合栈计算机(UVa12096)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- PyQt5调入数据库数据在表格中显示
数据库为Postgresql import sys from form import Ui_Form from PyQt5.Qt import QWidget, QApplication,QTable ...
- mysql 插入时带判断条件
INSERT INTO table (f1 ,f2 ,f3) ,'a', FROM DUAL WHERE NOT EXISTS ( FROM table2 where a = b) DUAL 为临时表 ...
- Two-stage rotation animation is deprecated. This application should use the smoother single-stage an
问题出在rootViewController同时包含UITabBarController和UINavigationController. 几经尝试,最后发现,在设置为window.rootViewCo ...