[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)方法传入. ...
随机推荐
- git —— bug分支
储藏工作现场 $ git stash 切换到需要修改bug的分支,创建临时分支 修复bug,修复完提交 修复完之后,切换到需要修改的分支.完成合并 合并后删除临时分支 完成后,可以重新回到没有修改完的 ...
- elasticsearch5.5
1.不能以root用户运行 groupadd es #增加es组 useradd es -g es -p pwd #增加es用户并附加到es组 chown -R e ...
- less常用样式集,清除浮动、背景自适应、背景渐变、圆角、内外阴影、高度宽度计算。
.clear-float() { content: ''; display: block; clear: both; height:; } //伪元素清除浮动 .after-clear() { &am ...
- python_selenium自动化测试框架
设计思路 本文整理归纳以往的工作中用到的东西,现汇总成基础测试框架提供分享. 框架采用python3 + selenium3 + PO + yaml + ddt + unittest等技术编写成基础测 ...
- TImage 显示 资源中 的图片、TResourceStream、资源文件
unit Unit5; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- Java测试框架Mockito源码分析
1.Mockito简介 测试驱动的开发(Test Driven Design, TDD)要求我们先写单元测试,再写实现代码.在写单元测试的过程中,一个很普遍的问题是,要测试的类会有很多依赖,这些依赖的 ...
- KnockoutJs学习笔记(一)
由于工作需要,接触到了Knockout,但是之前对于前台开发真的是不太了解,只能是摸着石头过河,边学边实践了. Knockout的官方网站是:http://knockoutjs.com/.我也是跟着官 ...
- Java项目中classpath路径
1.src不是classpath, WEB-INF/classes.lib.resources才是classpath,WEB-INF/是资源目录, 客户端不能直接访问. 2.WEB-INF/class ...
- spring boot之org.springframework.boot.context.TypeExcludeFilter
曾经碰到过这样一种情况,想让某个使用了spring 注解的类不被spring扫描注入到spring bean池中,比如下面的类使用了@Component和@ConfigurationPropertie ...
- Gitlab服务器维护
一. 内容 Gitlab服务器的更新 Gitlab服务器备份与恢复 导入Git仓库 二. Gitlab服务器的更新 1. 使用SSH登陆Gitlab服务器 2. 停止后端的unicorn服务器 [ro ...