当前位置: 首页 > news >正文

WPF下载远程文件,并显示进度条和百分比

WPF下载远程文件,并显示进度条和百分比

1、xaml

<ProgressBar HorizontalAlignment="Left" Height="10" Margin="96,104,0,0" Name="pbDown" VerticalAlignment="Top" Width="100"/>
<Label Content="Label" Name="label1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="206,104,0,0"/>

 2、CS程序

using System;
using System.Windows;
using System.Windows.Controls;
using System.Net;
using System.IO;
using System.Threading;
using System.Drawing;

namespace WpfDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            if (HttpFileExist("http://183.62.138.31:57863/opt/resources/%E9%A3%8E%E6%99%AF/f1.jpg"))
            {
                DownloadHttpFile("http://183.62.138.31:57863/opt/resources/%E9%A3%8E%E6%99%AF/f1.jpg", @"d:\f1.jpg");
            }
        }
        public void DownloadHttpFile(String http_url, String save_url)
        {
            WebResponse response = null;
            //获取远程文件
            WebRequest request = WebRequest.Create(http_url);
            response = request.GetResponse();
            if (response == null) return;
            //读远程文件的大小
            pbDown.Maximum = response.ContentLength;
            //下载远程文件
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                Stream netStream = response.GetResponseStream();
                Stream fileStream = new FileStream(save_url, FileMode.Create);
                byte[] read = new byte[1024];
                long progressBarValue = 0;
                int realReadLen = netStream.Read(read, 0, read.Length);
                while (realReadLen > 0)
                {
                    fileStream.Write(read, 0, realReadLen);
                    progressBarValue += realReadLen;
                    pbDown.Dispatcher.BeginInvoke(new ProgressBarSetter(SetProgressBar), progressBarValue);
                    realReadLen = netStream.Read(read, 0, read.Length);
                }
                netStream.Close();
                fileStream.Close();

            }, null);
        }        
        /// <summary>
        ///  判断远程文件是否存在
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <returns>存在-true,不存在-false</returns>
        private bool HttpFileExist(string http_file_url)
        {
            WebResponse response = null;
            bool result = false;//下载结果
            try
            {
                response = WebRequest.Create(http_file_url).GetResponse();
                result = response == null ? false : true;
            }
            catch (Exception ex)
            {
                result = false;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            return result;
        }
        public delegate void ProgressBarSetter(double value);
        public void SetProgressBar(double value)
        {
            //显示进度条
            pbDown.Value = value;
            //显示百分比
            label1.Content = (value / pbDown.Maximum) * 100 + "%";
        }
    }
}

 

相关文章:

  • iptables练习题
  • SQL Server 用链接服务器 同步MySQL
  • 实现app上对csdn的文章查看,以及文章中图片的保存 (制作csdn app 完结篇)
  • Linq动态条件
  • 详解java1.5新添特性------注解
  • Windows下批量设置SVN用户组权限
  • windows中eclipse打开当前文件所在文件夹
  • Effective C++: auto类型推断.
  • 利用CSS-border属性实现圆饼图表
  • 大圣归来:我们心中缺少一份英雄主义
  • Java命令学习系列(7):Javap(转)
  • 用VS2010编C#程序扫盲
  • 微软宣布Visual Studio 2015后继版本,开发者可以使用Visual C++ for Linux了
  • fullPage.js全屏滚动插件 中文帮助文档API
  • 9个 SSH常用命令选项(转载)
  • @jsonView过滤属性
  • Create React App 使用
  • css系列之关于字体的事
  • ES2017异步函数现已正式可用
  • Java,console输出实时的转向GUI textbox
  • javascript数组去重/查找/插入/删除
  • PHP 使用 Swoole - TaskWorker 实现异步操作 Mysql
  • 构造函数(constructor)与原型链(prototype)关系
  • 京东美团研发面经
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 浅谈web中前端模板引擎的使用
  • 收藏好这篇,别再只说“数据劫持”了
  • 线性表及其算法(java实现)
  • 一文看透浏览器架构
  • 正则学习笔记
  • 湖北分布式智能数据采集方法有哪些?
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (1)(1.9) MSP (version 4.2)
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (转载)(官方)UE4--图像编程----着色器开发
  • .Net - 类的介绍
  • .NET Core 和 .NET Framework 中的 MEF2
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .NET Core引入性能分析引导优化
  • .Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择)
  • .so文件(linux系统)
  • /etc/apt/sources.list 和 /etc/apt/sources.list.d
  • @Builder用法
  • [2018][note]用于超快偏振开关和动态光束分裂的all-optical有源THz超表——
  • [AI]文心一言爆火的同时,ChatGPT带来了这么多的开源项目你了解吗
  • [Apio2012]dispatching 左偏树
  • [docker] Docker的数据卷、数据卷容器,容器互联
  • [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
  • [Gamma]阶段测试报告
  • [GYCTF2020]Ez_Express
  • [halcon案例2] 足球场的提取和射影变换
  • [JavaWeb]—Spring入门