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 + "%";
}
}
}

WPF下载远程文件,并显示进度条和百分比的更多相关文章

  1. C# WinFrom 导入Excel文件,显示进度条

    因为WINForm程序是在64位上运行如果使用另外一种快速的读取Excel的方法会报“未在本地计算机上注册“Microsoft.Jet.OLEDB.12.0”提供程序” 所以我就换了现在这种读取有点慢 ...

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

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

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

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

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

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

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

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

  6. ajax 上传文件,显示进度条,进度条100%,进度条隐藏,出现卡顿就隐藏进度条,显示正在加载,再显示上传完成

    <form id="uploadForm" method="post" enctype="multipart/form-data"&g ...

  7. linux c++下载http文件并显示进度<转>

    #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...

  8. VC下载文件 + 显示进度条

    在codeproject里找了许久,发现这样一个VC下载文件并显示进度条的源码,于是添加了些中文注释: 1.下载线程函数: UINT DownloadFile(LPVOID pParam) { CWn ...

  9. VC下载文件显示进度条

    VC下载文件显示进度条 逗比汪星人2009-09-18上传   by Koma http://blog.csd.net/wangningyu http://download.csdn.net/deta ...

随机推荐

  1. fgetc和fputc函数

    1.输入函数 以下三个函数可用于一次读一个字符. #include <stdio.h> int getc( FILE *fp ); int fgetc( FILE *fp ); int g ...

  2. 气象API(2)

    中华万年历: http://wthrcdn.etouch.cn/weather_mini?city=北京通过城市名字获得天气数据,json数据http://wthrcdn.etouch.cn/weat ...

  3. ORACLE 學習筆記

    proc 里的 commit等于提交就是你做了insert或者update后,commit后才是真正修改或者插入了数据库中 如果不提交的话,那么这个表就被锁了 CURSOR MYCURSOR is   ...

  4. 配置ubuntu 16.04.1 LTS odoo 10.0开发环境

    使用VMware Fusion 8.5.0创建ubuntu 64bit虚拟机:使用ubuntu-16.04.1-desktop-amd64.iso镜像缺省安装ubuntu,用户名odoo,密码1234 ...

  5. 编译nginx时提示undefined reference to 'pcre_free_study' 的问题及解决

    ./configure --add-module=../ngx_devel_kit-0.2.19/ --add-module=../lua-nginx-module-0.9.19/  --with-l ...

  6. 团队项目——站立会议 DAY1

    团队项目--站立会议 DAY1        团队成员介绍(5人):张靖颜.何玥.钟灵毓秀.赵莹.王梓萱        今日(2016/5/6)为站立会议的第一天,一起对团队项目进行讨论,并对每个人的 ...

  7. windows下使用批处理脚本实现多个版本的JDK切换

    一.JDK版本切换批处理脚本 我们平时在window上做开发的时候,可能需要同时开发两个甚至多个项目,有时不同的项目对JDK的版本要求有区别,这时候我们可能会在一台电脑上安装多个版本的JDK,如下图所 ...

  8. [MFC] MFC音乐播放器 傻瓜级教程 网络 搜索歌曲 下载

    >目录< >——————————————————————< 1.建立工程  1.建立一个MFC工程,命名为Tao_Music 2.选择为基本对话框 3.包含Windows So ...

  9. Java-认识字符集-转载

    问题起源 对于计算机而言,它仅认识两个0和1,不管是在内存中还是外部存储设备上,我们所看到的文字.图片.视频等等“数据”在计算机中都是已二进制形式存在的.不同字符对应二进制数的规则,就是字符的编码.字 ...

  10. Sublime Text 设置文件详解

     Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自 ...