稳扎稳打Silverlight(20) - 2.0通信之WebClient, 以字符串的形式上传/下载数据, 以流的方式上传/下载数据

简介:
[索引页]
[源码下载]


稳扎稳打Silverlight(20) - 2.0通信之WebClient, 以字符串的形式上传/下载数据, 以流的方式上传/下载数据


作者: webabcd


介绍
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
http://webabcd.blog.51cto.com/1787395/342779  


示例
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> 
                 
                <StackPanel Margin="5" Width="200"> 
                        <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> 
                 
        </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()); 
                        } 
                } 
 
 
 
                /// <summary> 
                /// 演示流式下载 
                /// </summary> 
                void DownloadStreamDemo() 
                { 
                        Uri uri = new Uri(_urlStream, UriKind.Absolute); 
 
                        /*    
                         * WebClient - 将数据发送到指定的 URI,或者从指定的 URI 接收数据的类 
                         *         IsBusy - 指定的web请求是否正在进行中 
                         *         CancelAsync() - 取消指定的异步操作    
                         *         OpenReadCompleted - 数据读取完毕后(包括取消操作及有错误发生时)所触发的事件。流的方式 
                         *         DownloadProgressChanged - 下载数据过程中所触发的事件。正在下载或下载完全部数据后会触发 
                         *         OpenReadAsync(Uri address, Object userToken) - 以流的形式下载指定的 URI 的资源 
                         *                 Uri address - 需要下载的资源地址 
                         *                 Object userToken - 用户标识 
                         */
 
 
                        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) 
                { 
                        /* 
                         * 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()); 
 
                        progressBarStream.Value = (double)e.ProgressPercentage; 
                } 
 
                void clientDownloadStream_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
                { 
                        /* 
                         * OpenReadCompletedEventArgs.Error - 该异步操作期间是否发生了错误 
                         * OpenReadCompletedEventArgs.Cancelled - 该异步操作是否已被取消 
                         * OpenReadCompletedEventArgs.Result - 下载后的 Stream 类型的数据 
                         * OpenReadCompletedEventArgs.UserState - 用户标识 
                         */
 
 
                        if (e.Error != null
                        { 
                                lblMsgStream.Text += e.Error.ToString(); 
                                return
                        } 
 
                        if (e.Cancelled != true
                        { 
                                System.Windows.Media.Imaging.BitmapImage imageSource = new System.Windows.Media.Imaging.BitmapImage(); 
                                imageSource.SetSource(e.Result); 
                                img.Source = imageSource; 
                        } 
                } 
        } 
}
 
 
 
 
 




     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/343127 ,如需转载请自行联系原作者
相关文章

热门文章

最新文章