首页 > 编程语言 >c# 下载文件并处理进度

c# 下载文件并处理进度

时间:2022-08-20 10:58:19浏览次数:34  
标签:下载 string c# System client 进度 new using WebClient

我们先来个Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            System.Net.WebClient client = new System.Net.WebClient();
            byte[] data = client.DownloadData("http://b.hiphotos.baidu.com/zhidao/pic/item/3c6d55fbb2fb4316641d646623a4462309f7d3af.jpg");//一个真正存放数据的地址,一般我们将连接存在数据库中,数据存放在数据服务器上
            //如果网站没有什么限制的话,这样就能得到网站的图片数据了
            string path =Application.StartupPath;
            FileStream fs = new FileStream(path+"\\x.jpg", FileMode.Create);   
            //将byte数组写入文件中
            fs.Write(data,0,data.Length);     
            fs.Close();
        }
    }
}

 这里关键就是 System.Net.WebClient 这个类的用法,官方文档网址:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.webclient?view=net-6.0

比如我们要处理下载进度:

// Sample call : DownLoadFileInBackground4 ("http://www.contoso.com/logs/January.txt");
public static void DownLoadFileInBackground4(string address)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);

    // Specify a DownloadFileCompleted handler here...

    // Specify a progress notification handler.
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback4);

    client.DownloadFileAsync(uri, "serverdata.txt");
}

private static void DownloadProgressCallback4(object sender, DownloadProgressChangedEventArgs e)
{
    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
        (string)e.UserState,
        e.BytesReceived,
        e.TotalBytesToReceive,
        e.ProgressPercentage);
}

 

标签:下载,string,c#,System,client,进度,new,using,WebClient
From: https://www.cnblogs.com/otkur/p/16607298.html

相关文章

  • codeforces963D. Frequency of String【哈希】
    我的腿让我停下,可是我的心却不许我这么做今天又是为了明知多半不可能的事情奔波一早,一天里,出了很多丑,犯了很多错,见了很多人,有了很多意想不到的收获,我选择了我的生存方式......
  • ARC100F口胡
    写一篇自己能看得懂的题解。。。。。。先考虑一个正难则反,用\(a\)序列出现过的次数减去在不好的序列里面的出现次数。前者显然是\(k^{n-m}(n-m+1)\),考虑后者的答案。......
  • 使用新版本Minio的SDK实现文件的上传和下载
    一、minio配置信息,为了方便,这里不写在配置文件直接用枚举importlombok.AllArgsConstructor;importlombok.Getter;/***作者:唐婉*时间:2022/8/1511:28*描述:m......
  • C++primer练习16.1-14
    练习16.1::实例化就是模板通过实际调用而确定类型及其运算,抽象到具体练习16.2template<typenameT>intcompare(constT&v1,constT&v2){if(v1<v2)return-1;......
  • js脚本之修改leetcode刷题样式
    js脚本之修改leetcode刷题样式作用:调整字体大小(默认字体太小了)隐藏提示(点击按钮显示)自动隐藏顶栏油猴脚本代码://==UserScript==//@name力......
  • MVCC
    原理总体上来讲MVCC的实现是基于ReadView版本链以及Undo日志实现的MVCC就是在使用READCOMMITTD、REPEATABLEREAD这两种隔离级别的事务在执行普通的SELECT操作时访问记......
  • JQuery_遍历for循环&each方法$全局each&forof讲解
    遍历js的遍历方式for(初始化值;循环结束条件;步长)JQuery遍历方式JQuery对象.each(callback)$.each(object,[callback])for..of;<!DOCTYPEhtml><html><hea......
  • IfcDocumentInformation
    IfcDocumentInformation 实体定义IfcDocumentInformation捕获外部文档的“元数据”。本规范未定义文件的实际内容;相反,它可以在Location属性之后找到。 可以使用IfcD......
  • let、const、var的区别
    块级作用域:let和const具有块级作用域;var不具有;块级作用域解决了ES5两个问题:内层变量可能覆盖外层变量用来计数的循环变量会泄露为全局变量变量提升:var存......
  • CF1336A
    题目链接  题目意思:给一个以\(1\)为根,\(n-1\)条双向边的树形结构,让我们选出\(k\)个节点作为出发点前往根节点\(1\),算出每一个出发点到根节点的路径上有多少个非出发点的......