微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

稳扎稳打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://www.voidcn.com/article/p-ounmxjds-tq.html 


示例
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,

                                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;

                        }

                }

        }

}
 
 
 
 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐