微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight通过WebService上传大文件可拓展为支持断点续传

   GOOGLE一遍,没发现网上有现成代码,没办法,只有对一些零散代码进行改造,得到了本文要达到的效果

      1:服务器端采用webservice;

      2:SilverLight端可同时选择多个文件

      3:显示每个文件上传进度,并可拓展为断点续传;

      本文源码下载地址:http://download.csdn.net/source/1893588

      

      首先,生成一个SILVERLIGHT应用程序,选择创建WEB。在WEB中新建WEBSERVICE:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6. using System.IO;  
  7. using System.Xml.Serialization;  
  8.   
  9. namespace SilverlightApplication6.Web  
  10. {  
  11.     /// <summary>  
  12.     /// WebService1 的摘要说明  
  13. /// </summary>  
  14.     [WebService(Namespace = "http://tempuri.org/")]  
  15.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  16.     [System.ComponentModel.ToolBoxItem(false)]  
  17. // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。  
  18. // [System.Web.Script.Services.ScriptService]  
  19.     public class WebService1 : System.Web.Services.WebService  
  20.     {  
  21.   
  22.         [WebMethod]  
  23.         string HelloWorld()  
  24.         {  
  25.             return "Hello World";  
  26.         }  
  27.         /// <summary>  
  28.         /// 大文件上传.<br></br>  
  29. /// 2009-05-12 YJ 定义函数.<br></br>  
  30. /// <p@R_404_6460@m name="fileName">上传文件名</p@R_404_6460@m>  
  31. /// <p@R_404_6460@m name="offSet">偏移</p@R_404_6460@m>  
  32. /// <p@R_404_6460@m name="intoBuffer">每次上传字节数组 单位KB</p@R_404_6460@m>  
  33. /// <returns>上传是否成功</returns>  
  34. bool Upload(string fileName, long offSet,153); background-color:inherit; font-weight:bold">byte[] intoBuffer)  
  35.             //指定上传文件夹+文件名(相对路径)  
  36.             string strPath = "./Resources/" + fileName;  
  37. //将相对路径转换成服务器的绝对路径  
  38.             strPath = Server.MapPath(strPath);  
  39. if (offSet < 0)  
  40.             {  
  41.                 offSet = 0;  
  42.             }  
  43. byte[] buffer = intoBuffer;  
  44. if (buffer != null)  
  45.             {  
  46.                 //读写文件文件流,支持同步读写也支持异步读写  
  47.                 FileStream filesstream = new FileStream(strPath, FileMode.OpenorCreate, FileAccess.ReadWrite);  
  48.                 filesstream.Seek(offSet, SeekOrigin.Begin);  
  49.                 filesstream.Write(buffer, 0, buffer.Length);  
  50.                 filesstream.Flush();  
  51.                 filesstream.Close();  
  52.                 filesstream.dispose();  
  53.                 return true;  
  54. false;  
  55.         }  
  56.     }  
  57. }  

      客户端代码后台

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.Collections;  
  • using System.Windows.Data;  
  • using System.Xml;  
  • namespace SilverlightApplication6  
  • {  
  •     public partial class MainPage : UserControl  
  •     {  
  •         #region 类变量  
  •         List<FilesClass> fl = new List<FilesClass>();  
  •         System.IO.FileStream str = null;  
  •         ServiceReference1.WebService1SoapClient x =  
  • new SilverlightApplication6.ServiceReference1.WebService1SoapClient();  
  •         FilesClass obj = null;  
  •         byte[] by = int iFileCount = 1;  
  • int offset = 0;  
  • double current = 0;  
  • int currentFileNum = 0;  
  •         #endregion  
  •  
  •         #region 事件  
  • public MainPage()  
  •         {  
  •             InitializeComponent();  
  • private void Mybutton_Click(object sender, RoutedEventArgs e)  
  •             OpenFileDialog op = new OpenFileDialog();  
  •             op.Multiselect =              op.ShowDialog();  
  • if (op.Files != null)  
  • foreach (FileInfo f in op.Files)  
  •                 {  
  •                     if (f.Length < 1048576000)  
  •                     {  
  •                         FilesClass obj = new FilesClass();  
  •                         obj.PropFileName = f;  
  •                         obj.PropNumber = iFileCount.ToString();  
  •                         fl.Add(obj);  
  •                         iFileCount++;  
  •                     }  
  • else  
  •                         MessageBox.Show("Max file size is 1 MB");  
  •                 }  
  •                 MessageBox.Show("Select File");  
  • void MyReset_Click(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; color:rgb(0, RoutedEventArgs e)  
  •             fl.Clear();  
  •             iFileCount = 1;  
  •             progFile2.Value = 0;  
  • void Upload_Click(             UpLoadStart(0);  
  •         #region 私有函数  
  • void UpLoadStart(int fileNum)  
  • try  
  •                 x.UploadCompleted += new EventHandler<SilverlightApplication6.ServiceReference1.UploadCompletedEventArgs>(x_UploadCompleted);  
  •                 if (fileNum == 0)  
  •                     currentFileNum = 0;  
  • if (fl.Count > 0 && fl.Count > fileNum)  
  •                 {  
  •                     obj = (FilesClass)fl[fileNum];  
  •                     str = obj.PropFileName.OpenRead();  
  •                     progFile2.Maximum = (double)(str.Length);  
  •                     UpLoadStep();  
  •                 }  
  •             }  
  • catch  
  • throw;  
  • void UpLoadStep()  
  • try  
  • if (str.Length - current > 10240)  
  •                     offset = 10240;  
  •                     by = new byte[offset];  
  •                     str.Read(by, offset);  
  •                     current = (double)(str.Position);  
  •                     x.UploadAsync(obj.PropFileName.Name, str.Position - offset, by);  
  •                     offset = (int)(str.Length - str.Position);  
  •                     by = byte[offset];  
  •                     str.Read(by, offset);  
  • void x_UploadCompleted(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; color:rgb(0, SilverlightApplication6.ServiceReference1.UploadCompletedEventArgs e)  
  • if (e.Result != true)  
  •                 MessageBox.Show("上传失败,请重新上传!");  
  • this.rre.Text = "" + e.Result.ToString();  
  •             progFile2.Value = current;  
  • if (current == str.Length)  
  •                 x.UploadCompleted -= this.x_UploadCompleted;  
  •                 str.Flush();  
  •                 str.Close();  
  •                 str.dispose();  
  •                 fl[currentFileNum].PropStatus = "上传成功!";  
  •                 //BindGrid();  
  •                 currentFileNum++;  
  •                 offset = 0;  
  •                 current = 0;  
  • if (fl.Count > currentFileNum)  
  •                     UpLoadStart(currentFileNum);  
  • else  
  •                     //MessageBox.Show("all end!");  
  • return;  
  •             UpLoadStep();  
  •     }  
  • }  
  •       前台

    <UserControl x:Class="SilverlightApplication6.MainPage"  

  •     xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"    
  •              xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"    
  •              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  •     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  •     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  •     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">  
  •     <Grid x:Name="LayoutRoot" Background="YellowGreen" ShowGridLines="True" >  
  •         <Grid.RowDeFinitions>  
  •             <RowDeFinition Height="535"></RowDeFinition>  
  •             <RowDeFinition Height="30"></RowDeFinition>  
  •             <RowDeFinition Height="35"></RowDeFinition>  
  •             <RowDeFinition Height="35"></RowDeFinition>  
  •         </Grid.RowDeFinitions>  
  •         <StackPanel Grid.Row="2" Orientation="Horizontal" >  
  •             <Button Content="选择上传文件" x:Name="MyButton" Width="100" Height="20" Click="Mybutton_Click" Grid.Column="0" FontSize="12"></Button>  
  •             <Button Content="清除" x:Name="MyReset" Width="100" Height="20" Click="MyReset_Click" Grid.Column="1" FontSize="12"></Button>  
  •             <Button Content="上传" x:Name="Upload" Width="100" Height="20" Click="Upload_Click"  Grid.Column="2" FontSize="12"></Button>  
  •         </StackPanel>  
  •         <StackPanel Grid.Row="1" VerticalAlignment="Center">  
  •             <ProgressBar Name="progFile2" Height="20" Width="600"></ProgressBar>  
  •         </StackPanel>  
  •         <TextBlock x:Name="rre" Width="200" Grid.Row="0"></TextBlock>  
  •     </Grid>  
  • </UserControl>  
  • 版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐