Silverlight文件上传下载实现方法(下载保存)

2019-12-30 11:12:34于丽

UC_UpDown.xaml.cs

 

 
  1. using System;  using System.Collections.Generic; 
  2. using System.Linq;  using System.Net; 
  3. using System.Windows;  using System.Windows.Controls; 
  4. using System.Windows.Documents;  using System.Windows.Input; 
  5. using System.Windows.Media;  using System.Windows.Media.Animation; 
  6. using System.Windows.Shapes;   
  7. using System.Windows.Media.Imaging; //因为要使用BitmapImage  using System.IO; //因为要使用Stream 
  8.   namespace HCLoad 
  9. {  public partial class UC_UpDown : UserControl 
  10. {  //1、WebClient 对象一次只能启动一个请求。如果在一个请求完成(包括出错和取消)前,即IsBusy为true时,进行第二个请求,则第二个请求将会抛出 NotSupportedException 类型的异常 
  11. //2、如果 WebClient 对象的 BaseAddress 属性不为空,则 BaseAddress 与 URI(相对地址) 组合在一起构成绝对 URI  //3、WebClient 类的 AllowReadStreamBuffering 属性:是否对从 Internet 资源接收的数据做缓冲处理。默认值为true,将数据缓存在客户端内存中,以便随时被应用程序读取 
  12.    
  13.   //获取选定图片信息 
  14. System.IO.FileInfo fileinfo;  public UC_UpDown() 
  15. {  InitializeComponent(); 
  16. }  #region 下载图片 
  17. private void btnDownload_Click(object sender, RoutedEventArgs e)  { 
  18. //向指定的Url发送下载流数据请求   String imgUrl = "http://www.easck.com//OpenReadCompletedEventArgs.Error - 该异步操作期间是否发生了错误  //OpenReadCompletedEventArgs.Cancelled - 该异步操作是否已被取消 
  19. //OpenReadCompletedEventArgs.Result - 下载后的 Stream 类型的数据  //OpenReadCompletedEventArgs.UserState - 用户标识 
  20.   if (e.Error != null) 
  21. {  MessageBox.Show(e.Error.ToString()); 
  22. return;  } 
  23. if (e.Cancelled != true)  { 
  24. //获取下载的流数据(在此处是图片数据)并显示在图片控件中  //Stream stream = e.Result;