Silverlight上传下载三种方法解析(三)

WebService思路及功能实现

实现思路:通过添加Web服务来实现

上传功能实现

一.获得本地文件并转化为字节数组传入到服务端

private void buttonUpload_Click(object sender, RoutedEventArgs e)

{

OpenFileDialog openFileDialog = new OpenFileDialog();

System.IO.Stream uploadStream = null;

openFileDialog.Filter = "Jpeg Files (*.jpg)|*.jpg|All Files(*.*)|*.*";

openFileDialog.Multiselect = false;

if (openFileDialog.ShowDialog()==true)

{

fileinfo = openFileDialog.File;

uploadStream = fileinfo.OpenRead();

byte[] buffer = new byte[uploadStream.Length];

//将选择的文件信息流写入到字节数组中

uploadStream.Read(buffer, 0, buffer.Length);

                 if (fileinfo!=null)

                 {

                        c.ActionUploadCompleted += new EventHandler<AsyncCompletedEventArgs>(c_ActionUploadCompleted);

                        c.ActionUploadAsync(fileinfo.Name, buffer, true);

                 }

         }

}

二.服务端捕获字节数组并还原文件

/// <summary>

/// 上传功能

/// </summary>

/// <param name="fileName">文件名</param>

/// <param name="fileData">字节流</param>

/// <param name="isApplend">是否追加</param>

[WebMethod]

public void ActionUpload(string fileName, byte[] fileData, bool isApplend)

{

             if (!Directory.Exists(uploadFolder))

             {

                   Directory.CreateDirectory(uploadFolder);

             }

             FileMode fileMode = isApplend ? FileMode.Append : FileMode.Create;

            using(FileStream fs=new FileStream(uploadFolder+@"\"+fileName,fileMode,FileAccess.Write))

           {

                      fs.Write(fileData,0,fileData.Length);

          }

}

下载功能实现

一.客户端传入下载文件名,服务端捕获文件名并传回该文件的字节数组

客户端代码:

private void buttonBase_Click(object sender, RoutedEventArgs e)

{

              UploadFileWebServiceSoapClient c2 = new UploadFileWebServiceSoapClient();

              SaveFileDialog saveDialog = new SaveFileDialog();

              saveDialog.Filter = "Jpeg Files (*.jpg)|*.jpg|All Files(*.*)|*.*";

             //给下载文件一个默认的Name

             saveDialog.DefaultFileName = "yl";

             if (listBox1.SelectedItems.Count < 0)

            {

                      MessageBox.Show("请选择要下载的下载文件");

             }

             else if (listBox1.SelectedItems.Count > 1)

             {

                      MessageBox.Show("暂不提供批量下载");

              }

               else

              {

                      if (saveDialog.ShowDialog()==true)

                     {

                               c2.getBinaryFileCompleted += new EventHandler<getBinaryFileCompletedEventArgs>(c2_getBinaryFileCompleted);

                                c2.getBinaryFileAsync(listBox1.SelectedItem.ToString(),saveDialog);

                      }

               }

     }

    服务端代码:

/// <summary>

/// 返回下载文件的字节流,直接返回一个Stream流怎么老是返回不了呢。悲哀

/// </summary>

/// <param name="filename"></param>

/// <returns></returns>

[WebMethod]

public byte[] getBinaryFile(string filename)

                 {

                          string downloadsorcepath = uploadFolder + @"\" + filename;

                          if (File.Exists(downloadsorcepath))

                         {

                                try

                               {

                                         FileStream s2 = File.OpenRead(downloadsorcepath);

                                          byte[] downloadbyte = new byte[s2.Length];

                                          s2.Read(downloadbyte, 0, downloadbyte.Length);

                                          s2.Close();

                                          return downloadbyte;

                               }

                              catch (Exception)

                              {

                                         return new byte[0];

                              }

                      }

                      else

                      {

                           return new byte[0];

                      }

}

二.客户端捕获服务端传回的字节数组,并还原文件保存在本地

void c2_getBinaryFileCompleted(object sender, getBinaryFileCompletedEventArgs e)

{

           if (e.Error==null)

          {

                byte[] mybyte = new byte[e.Result.Length];

                mybyte = e.Result;

                SaveFileDialog saveDialog = (SaveFileDialog)e.UserState;

               using (Stream stream =saveDialog.OpenFile())

               {

                     stream.Write(mybyte, 0, mybyte.Length);

               }

          }

}

转载于:https://www.cnblogs.com/yidifanhua/archive/2011/07/31/2122721.html

Silverlight多文件(大文件)上传项目源码 一个免费的SL多文件上传,支持大文件上传 Features(特色): - Select multiple files to upload (选择多文件上传) - Upload multiple files at the same time (asynchonous)(一次上传多文件,异步刷新) - Cancel a single upload, or clear the full list(可以取消单个上传或清空上传列表) - Shows the overall progress(显示所有上传进度条) - Error notification when upload failed(上传错误会有提示) For developers / webmasters(对于开发者或者网管): - Configure file extension filter (for example, only .jpg files) (配置支持的格式,如仅仅支持jpg) - Configure maximum file size (设置最大上传尺寸) - Configure the number of simultaneous uploads (设置一次最多上传数量) - Input for custom parameters (输入自定义参数) - Basic webservice included - Change the default color by setting a single parameter(可以改变默认演示) - JavaScript events and properties for integration in your website Free Silverlight Multi File Uploader 配置参数说明: MaxFileSizeKB: File size in KBs.(最大上传尺寸,以KB为单位) MaxUploads: Maximum number of simultaneous uploads(一次上传最多的文件数) FileFilter: File filter, for example ony jpeg use: FileFilter=Jpeg (*.jpg) |*.jpg(上传文件格式) CustomParam: Your custom parameter, anything here will be available in the WCF webservice DefaultColor: The default color for the control, for example: LightBlue Possible parameters: <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/mpost.SilverlightMultiFileUpload.xap" MinimumVersion="2.0.30523" Width="415" Height="280" InitParameters="MaxFileSizeKB=1000,MaxUploads=2,FileFilter=,CustomParam=1,DefaultColor=LightBlue" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值