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. css3动画属性(transitions:property duration timing transition-delay)

    transitions:property duration timing-function; transitionst他有三个参数:1) property:属性设置,例如background,colo ...

  2. Java文件内容的复制

    package a.ab; import java.io.*; public class FileReadWrite { public static void main(String[] args) ...

  3. java抓取网页数据,登录之后抓取数据。

    最近做了一个从网络上抓取数据的一个小程序.主要关于信贷方面,收集的一些黑名单网站,从该网站上抓取到自己系统中. 也找了一些资料,觉得没有一个很好的,全面的例子.因此在这里做个笔记提醒自己. 首先需要一 ...

  4. pro生成sln

    跳转到对应的工程目录,通过执行如下的命令:qmake -tp vc 命令实现

  5. 第44讲:Scala中View Bounds代码实战及其在Spark中的应用源码解析

    今天学习了view bounds的内容,来看下面的代码. //class Pair[T <: Comparable[T]](val first : T,val second : T){//  d ...

  6. nginx安装启动

    参考:http://network.51cto.com/art/201005/198198_4.htm 下载nginx tar.gz安装包下载pcre tar.gz安装包 安装pcretar zxvf ...

  7. JQuery学习(选择器-基本-*)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  8. oracle密码文件管理

    密码文件 密码文件作用: 密码文件用于dba用户的登录认证. dba用户:具备sysdba和sysoper权限的用户,即oracle的sys和system用户. 本地登录: 1)操作系统认证: [or ...

  9. libqrencode 3.4.3 发布,二维码的C解析库

    libqrencode 3.4.3 的命令行增加了 --rle 参数,修复了开发库和命令行工具的一些小 bug. libqrencode (QRencode) 是一个用C语言编写的用来解析二维条形码( ...

  10. Dynamic CRM 2013学习笔记(三十一)自定义用excel批量导入实体数据

    有一个实体的子表数据量太大,于是客户想用execel来导入实体数据.首先想到的是用系统自带的Import Data,客户嫌太麻烦,比如lookup字段要做map等. 下面是具体的实现步骤: 一.定义e ...