<a href="http://webabcd.blog.51cto.com/1787395/342790" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>
穩紮穩打Silverlight(20) - 2.0通信之WebClient, 以字元串的形式上傳/下載下傳資料, 以流的方式上傳/下載下傳資料
介紹
Silverlight 2.0 詳解WebClient,以字元串的形式上傳、下載下傳資料;以流的方式上傳、下載下傳資料
WebClient - 将資料發送到指定的 URI,或者從指定的 URI 接收資料的類
DownloadStringAsync(Uri address, Object userToken) - 以字元串的形式下載下傳指定的 URI 的資源
UploadStringAsync(Uri address, string data) - 以字元串的形式上傳資料到指定的 URI。所使用的 HTTP 方法預設為 POST
OpenReadAsync(Uri address, Object userToken) - 以流的形式下載下傳指定的 URI 的資源
OpenWriteAsync(Uri address, string method, Object userToken) - 打開流以使用指定的方法向指定的 URI 寫入資料
線上DEMO
示例
1、以字元串的形式和流的形式下載下傳資料
WebClientDownload.xaml
<UserControl x:Class="Silverlight20.Communication.WebClientDownload"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<StackPanel Margin="5" Width="200">
<TextBox x:Name="lblMsgString" Margin="5" />
<ProgressBar x:Name="progressBarString" Height="20" Margin="5" Minimum="0" Maximum="100" />
</StackPanel>
<TextBox x:Name="lblMsgStream" Margin="5" />
<ProgressBar x:Name="progressBarStream" Height="20" Margin="5" Minimum="0" Maximum="100" />
<Image x:Name="img" Margin="5" />
</StackPanel>
</UserControl>
WebClientDownload.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
namespace Silverlight20.Communication
{
public partial class WebClientDownload : UserControl
{
// 用于示範以字元串的形式下載下傳資料
string _urlString = "http://localhost/Files/Demo.zip";
// 用于示範以流的形式下載下傳資料
string _urlStream = "http://localhost/Files/Logo.png";
public WebClientDownload()
{
InitializeComponent();
// 示範字元串式下載下傳
DownloadStringDemo();
// 示範流式下載下傳
DownloadStreamDemo();
}
/// <summary>
/// 示範字元串式下載下傳
/// </summary>
void DownloadStringDemo()
Uri uri = new Uri(_urlString, UriKind.Absolute);
/*
* WebClient - 将資料發送到指定的 URI,或者從指定的 URI 接收資料的類
* DownloadStringCompleted - 下載下傳資料完畢後(包括取消操作及有錯誤發生時)所觸發的事件
* DownloadProgressChanged - 下載下傳資料過程中所觸發的事件。正在下載下傳或下載下傳完全部資料後會觸發
* DownloadStringAsync(Uri address, Object userToken) - 以字元串的形式下載下傳指定的 URI 的資源
* Uri address - 需要下載下傳的資源位址
* Object userToken - 使用者辨別
*/
System.Net.WebClient clientDownloadString = new System.Net.WebClient();
clientDownloadString.DownloadStringCompleted += new DownloadStringCompletedEventHandler(clientDownloadString_DownloadStringCompleted);
clientDownloadString.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clientDownloadString_DownloadProgressChanged);
clientDownloadString.DownloadStringAsync(uri, "userToken");
void clientDownloadString_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
/*
* DownloadProgressChangedEventArgs.ProgressPercentage - 下載下傳完成的百分比
* DownloadProgressChangedEventArgs.BytesReceived - 目前收到的位元組數
* DownloadProgressChangedEventArgs.TotalBytesToReceive - 總共需要下載下傳的位元組數
* DownloadProgressChangedEventArgs.UserState - 使用者辨別
lblMsgString.Text = string.Format("下載下傳完成的百分比:{0}\r\n目前收到的位元組數:{1}\r\n總共需要下載下傳的位元組數:{2}\r\n",
e.ProgressPercentage.ToString() + "%",
e.BytesReceived.ToString(),
e.TotalBytesToReceive.ToString());
progressBarString.Value = (double)e.ProgressPercentage;
void clientDownloadString_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
* DownloadStringCompletedEventArgs.Error - 該異步操作期間是否發生了錯誤
* DownloadStringCompletedEventArgs.Cancelled - 該異步操作是否已被取消
* DownloadStringCompletedEventArgs.Result - 下載下傳後的字元串類型的資料
* DownloadStringCompletedEventArgs.UserState - 使用者辨別
if (e.Error != null)
{
lblMsgString.Text += e.Error.ToString();
return;
}
if (e.Cancelled != true)
lblMsgString.Text += string.Format("使用者辨別:{0}", e.UserState.ToString());
/// 示範流式下載下傳
void DownloadStreamDemo()
Uri uri = new Uri(_urlStream, UriKind.Absolute);
* IsBusy - 指定的web請求是否正在進行中
* CancelAsync() - 取消指定的異步操作
* OpenReadCompleted - 資料讀取完畢後(包括取消操作及有錯誤發生時)所觸發的事件。流的方式
* OpenReadAsync(Uri address, Object userToken) - 以流的形式下載下傳指定的 URI 的資源
System.Net.WebClient clientDownloadStream = new System.Net.WebClient();
if (clientDownloadStream.IsBusy)
clientDownloadStream.CancelAsync();
clientDownloadStream.OpenReadCompleted += new OpenReadCompletedEventHandler(clientDownloadStream_OpenReadCompleted);
clientDownloadStream.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clientDownloadStream_DownloadProgressChanged);
clientDownloadStream.OpenReadAsync(uri);
void clientDownloadStream_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
progressBarStream.Value = (double)e.ProgressPercentage;
void clientDownloadStream_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
* OpenReadCompletedEventArgs.Error - 該異步操作期間是否發生了錯誤
* OpenReadCompletedEventArgs.Cancelled - 該異步操作是否已被取消
* OpenReadCompletedEventArgs.Result - 下載下傳後的 Stream 類型的資料
* OpenReadCompletedEventArgs.UserState - 使用者辨別
lblMsgStream.Text += e.Error.ToString();
System.Windows.Media.Imaging.BitmapImage imageSource = new System.Windows.Media.Imaging.BitmapImage();
imageSource.SetSource(e.Result);
img.Source = imageSource;
}
}
<a href="http://webabcd.blog.51cto.com/1787395/343126" target="_blank">未完待續>></a>
OK
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/343127,如需轉載請自行聯系原作者