没有合适的资源?快使用搜索试试~ 我知道了~
silverlight 使用webclient 实现上传和下载数据流
5星 · 超过95%的资源 需积分: 10 36 下载量 101 浏览量
2014-02-25
10:28:41
上传
评论
收藏 452KB PDF 举报
温馨提示
试读
17页
SilverLight 学习笔记 使用WebClient实现通讯 二 上传和下载流数据
资源推荐
资源详情
资源评论
SilverLight 学习笔记--使用WebClient实现通讯(二)(上传和下载流数据)
接上节,本文直接进入实例操作部分。在此处,我们以典型的图片文件为例来学习
如何用WebClient类来实现上传和下载流数据的。
新建一个Silverlight应用程序,命名为:SLWebClientStream。
一、准备工作:
1、准备一张图片,本例我们准备了一张名为Bubble.jpg的图片,把它拷贝到SLWe
bClientStream.Web项目的根目录下。
2、在SLWebClientStream.Web项目下新建一个目录,命名为Pics,我们将在此处
放置上传上来的图片文件。
完成准备工作后,项目如下图:
二、编码实现
(一)、客户端部分
1、建立用户界面
P 1 / 17
SilverLight 学习笔记--使用WebClient实现通讯(二)(上传和下载流数据)
Page.xaml内容如下:
<UserControl x:Class="SLWebClientStream.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pres
entation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="500" Height="450">
<StackPanel Background="White" Height="450">
<TextBlock x:Name="tbMsgString" Text="????" TextAlig
nment="Center" Foreground="Green"></TextBlock>
<Button x:Name="btnDownload" Content="DownLoad Pictu
res" Width="150" Height="35" Margin="15" Click="btnDownload_
Click"/>
<Border Background="Wheat" BorderThickness="5" Width
="400" Height="280">
<Image x:Name="imgDownLoad" Width="400" Height="
300" Margin="15" Stretch="Fill"/>
</Border>
<Button x:Name="btnUpLoad" Content="UpLoad Pictures"
Width="150" Height="35" Margin="15" Click="btnUpLoad_Click"
/>
</StackPanel>
</UserControl>
界面如下:
P 2 / 17
SilverLight 学习笔记--使用WebClient实现通讯(二)(上传和下载流数据)
2、下载图片文件代码
#region ????
private void btnDownload_Click(object sender, Routed
EventArgs e)
{
//????Url?????????
String imgUrl = "http://localhost:51896/Bubble.j
pg";
Uri endpoint = new Uri(imgUrl);
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadComplete
dEventHandler(OnOpenReadCompleted);
client.DownloadProgressChanged += new DownloadPr
ogressChangedEventHandler(clientDownloadStream_DownloadProgr
essChanged);
client.OpenReadAsync(endpoint);
P 3 / 17
SilverLight 学习笔记--使用WebClient实现通讯(二)(上传和下载流数据)
}
void OnOpenReadCompleted(object sender,OpenReadCompl
etedEventArgs e)
{
//OpenReadCompletedEventArgs.Error - ??????????
????
//OpenReadCompletedEventArgs.Cancelled - ??????
?????
//OpenReadCompletedEventArgs.Result - ???? Stre
am ?????
//OpenReadCompletedEventArgs.UserState - ????
if (e.Error != null)
{
MessageBox.Show(e.Error.ToString());
return;
}
if (e.Cancelled != true)
{
//????????(????????)?????????
Stream stream = e.Result;
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
imgDownLoad.Source = bitmap;
}
}
void clientDownloadStream_DownloadProgressChanged(ob
ject sender, DownloadProgressChangedEventArgs e)
{
//DownloadProgressChangedEventArgs.ProgressPerce
ntage - ????????
//DownloadProgressChangedEventArgs.BytesReceived
- ????????
//DownloadProgressChangedEventArgs.TotalBytesToR
P 4 / 17
剩余16页未读,继续阅读
资源评论
- z32149652014-02-25多少 有点用 要是程序的形式 就更好了
- kkuu882015-09-18还是能用的,有点启发性
- barton1314202014-08-09好象都是乱码。是CSDN处理文档的问题?
Abby66666
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功