您的位置:首页 > 其它

2.0通信之WebClient, 以字符串的形式上传/下载数据, 以流的方式上传/下载数据

2009-06-08 22:04 337 查看
介绍
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
/article/4589581.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
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

using System.ServiceModel.Web;
using System.Collections.Generic;
using System.Text;
using System.IO;

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class REST

WebClientUpload.xaml

<UserControl x:Class="Silverlight20.Communication.WebClientUpload"
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" />
<Button x:Name="btnString" Content="上传文件(字符串的方式)" Margin="5" Click="btnString_Click" />
</StackPanel>

<StackPanel Margin="5" Width="200">
<TextBox x:Name="lblMsgStream" Margin="5" />
<ProgressBar x:Name="progressBarStream" Height="20" Margin="5" Minimum="0" Maximum="100" />
<Button x:Name="btnStream" Content="上传文件(流的方式)" Margin="5" Click="btnStream_Click" />
</StackPanel>

</StackPanel>
</UserControl>

WebClientUpload.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;
using System.Windows.Resources;
using System.ComponentModel;
using System.Windows.Browser;

namespace Silverlight20.Communication
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐