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

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

概述[索引页] [源码下载] 稳扎稳打Silverlight(20) - 2.0通信之WebClient, 以字符串的形式上传/下载数据, 以流的方式上传/下载数据 作者:webabcd 介绍 Silverlight 2.0 详解WebClient,以字符串的形式上传、下载数据;以流的方式上传、下载数据     WebClient - 将数据发送到指定的 URI,或者从指定的 URI 接收数据的类   [索引页]
[源码下载]


稳扎稳打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;

                        }

                }

        }

}    
  未完待续>>   OK
[源码下载]
总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(20) - 2.0通信之WebClient, 以字符串的形式上传/下载数据, 以流的方式上传/下载数据全部内容,希望文章能够帮你解决稳扎稳打Silverlight(20) - 2.0通信之WebClient, 以字符串的形式上传/下载数据, 以流的方式上传/下载数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1052410.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存