摘要

在项目开发中经常会用到下载文件,这里使用winform实现了一个带进度条的例子。

一个例子

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace Wolfy.DownLoad
  14. {
  15. public partial class MainFrm : Form
  16. {
  17. private string _saveDir;
  18. public MainFrm()
  19. {
  20. InitializeComponent();
  21. _saveDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");
  22. }
  23.  
  24. private void MainFrm_Load(object sender, EventArgs e)
  25. {
  26.  
  27. if (!Directory.Exists(_saveDir))
  28. {
  29. Directory.CreateDirectory(_saveDir);
  30. }
  31.  
  32. }
  33.  
  34. private void btnDownLoad_Click(object sender, EventArgs e)
  35. {
  36. string imageUrl = "http://image.3761.com/nvxing/2016022921/2016022921382152113.jpg";
  37.  
  38. string fileExt = Path.GetExtension(imageUrl);
  39. string fileNewName = Guid.NewGuid() + fileExt;
  40. bool isDownLoad = false;
  41. string filePath = Path.Combine(_saveDir, fileNewName);
  42. if (File.Exists(filePath))
  43. {
  44. isDownLoad = true;
  45. }
  46. var file = new FileMessage
  47. {
  48. FileName = fileNewName,
  49. RelativeUrl = "nvxing/2016022921/2016022921382152113.jpg",
  50. Url = imageUrl,
  51. IsDownLoad = isDownLoad,
  52. SavePath = filePath
  53. };
  54.  
  55. if (!file.IsDownLoad)
  56. {
  57.  
  58. string fileDirPath = Path.GetDirectoryName(file.SavePath);
  59. if (!Directory.Exists(fileDirPath))
  60. {
  61. Directory.CreateDirectory(fileDirPath);
  62. }
  63. try
  64. {
  65. WebClient client = new WebClient();
  66. client.DownloadFileCompleted += client_DownloadFileCompleted;
  67. client.DownloadProgressChanged += client_DownloadProgressChanged;
  68. client.DownloadFileAsync(new Uri(file.Url), file.SavePath, file.FileName);
  69. }
  70. catch
  71. {
  72.  
  73. }
  74.  
  75. }
  76. }
  77. void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  78. {
  79. if (e.UserState != null)
  80. {
  81. this.lblMessage.Text = e.UserState.ToString() + ",下载完成";
  82. }
  83. }
  84.  
  85. void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  86. {
  87. this.proBarDownLoad.Minimum = ;
  88. this.proBarDownLoad.Maximum = (int)e.TotalBytesToReceive;
  89. this.proBarDownLoad.Value = (int)e.BytesReceived;
  90. this.lblPercent.Text = e.ProgressPercentage + "%";
  91. }
  92. }
  93. }

测试

[c#]WebClient异步下载文件并显示进度的更多相关文章

  1. 实现在 .net 中使用 HttpClient 下载文件时显示进度

    在 .net framework 中,要实现下载文件并显示进度的话,最简单的做法是使用 WebClient 类.订阅 DownloadProgressChanged 事件就行了. 但是很可惜,WebC ...

  2. WebClient异步下载文件

    namespace ConsoleAppSyncDownload{    class Program    { static void Main(string[] args)        {     ...

  3. C# Winform下载文件并显示进度条

    private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...

  4. Winform下载文件并显示进度条

    本来是要研究怎样判断下载完成,结果找到这个方法,可以在这个方法完成之后提示下载完成. 代码如下: using System; using System.Collections.Generic; usi ...

  5. 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具

    转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...

  6. 通过HttpUrlConnection下载文件并显示进度条

    实现效果: 核心下载块: int count = 0; URL url = new URL("http://hezuo.downxunlei.com/xunlei_hezuo/thunder ...

  7. WebClient.DownloadFile(线程机制,异步下载文件)

    线程机制(避免卡屏),异步下载文件. 我做网站的监控,WebClient.DownloadFile这个方法是我经常用到的,必要的时候肯定是要从网上下载些什么(WebRequest 也可以下载网络文件, ...

  8. 一个简单的利用 WebClient 异步下载的示例(二)

    继上一篇 一个简单的利用 WebClient 异步下载的示例(一) 后,我想把核心的处理提取出来,成 SkyWebClient,如下: 1. SkyWebClient 该构造函数中 downloadC ...

  9. AsyncTask用法解析-下载文件动态更新进度条

    1. 泛型 AysncTask<Params, Progress, Result> Params:启动任务时传入的参数,通过调用asyncTask.execute(param)方法传入. ...

随机推荐

  1. DOS命令大全(一)

    #1 一: net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" ...

  2. Java 容器的打印

    Java容器类库中的两种主要类型,它们的区别在于容器中每个"槽"保存的元素个数 Clollection容器只能在保存一个元素,此类容器包括: List,它以特定顺序保存一组元素 S ...

  3. linux下nc提交web报文问题

    1.用wireshark截取访问百度首页拿到的请求数据包: GET /index.php?tn=newbdie_bd_dg&bar= HTTP/1.1 Host: www.baidu.com ...

  4. babel使用中不想使用 严格模式 如何去除?

    使用babel进行es6转es5时,默认转化之后是严格模式,有些时候我们想去除严格模式. 解决方法如下 安装 babel-plugin-transform-remove-strict-mode 依赖 ...

  5. bzoj 1233

    先要了解一个结论,在多种可行的堆叠方案中,至少有一种能使层数最高的方案同时使得底边最短.即底边最短的,层数一定最高. dp[ i ] = min(sum[j - 1] - sum[i - 1])  j ...

  6. bzoj 1295 1295: [SCOI2009]最长距离

    思路:对于每个点出发bfs做一次dp, dp[ i ][ j ][ k ] 表示从枚举的该点能不能经过k个障碍物到达(i , j). #include<bits/stdc++.h> #de ...

  7. HDU - 1716 排列2 水题

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. CSS基础-DAY1

    CSS 概述CSS 指层叠样式表 (Cascading Style Sheets),样式定义了如何显示 HTML文件中的标签元素,CSS是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标 ...

  9. python的可变与不可变数据类型

    <python的可变与不可变数据类型>     python与C/C++不一样,它的变量使用有自己的特点,当初学python的时候,一定要记住“一切皆为对象,一切皆为对象的引用”这句话,其 ...

  10. C# NPOCO 轻量级ORM框架(进阶)

    继续翻译NPOCO wiki. 这篇将home上 下面的几个页面翻译. wiki地址:https://github.com/schotime/NPoco/wiki 上一篇: http://www.cn ...