zoukankan      html  css  js  c++  java
  • 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 + "%";
            }
        }
    }
    
  • 相关阅读:
    【随笔】野生在左 科班在右——数据结构学习誓师贴
    javascript基础修炼(7)——Promise,异步,可靠性
    express中间件系统的基本实现
    javascript基础修炼(6)——前端路由的基本原理
    javascript基础修炼(5)—Event Loop(Node.js)
    一统江湖的大前端(7)React.js-从开发者到工程师
    一统江湖的大前端(6)commander.js + inquirer.js——懒,才是第一生产力
    一统江湖的大前端(5)editorconfig + eslint——你的代码里藏着你的优雅
    Jmeter接口测试之用户自定义变量(九)
    Jmeter4.0接口测试之案例实战(七)
  • 原文地址:https://www.cnblogs.com/sntetwt/p/5310520.html
Copyright © 2011-2022 走看看