Google一遍,没发现网上有现成代码,没办法,只有对一些零散代码进行改造,得到了本文要达到的效果:
1:服务器端采用webservice;
2:Silverlight端可同时选择多个文件;
3:显示每个文件的上传进度,并可拓展为断点续传;
本文源码下载地址:http://download.csdn.net/source/1893588
首先,生成一个SILVERliGHT应用程序,选择创建WEB。在WEB中新建WEBSERVICE:
view plain using System; using System.Collections.Generic; using System.linq; using System.Web; using System.Web.Services; using System.IO; using System.Xml.Serialization; namespace SilverlightApplication6.Web { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolBoxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] string HelloWorld() { return "Hello World"; } /// <summary> /// 大文件上传.<br></br> /// 2009-05-12 YJ 定义函数.<br></br> /// <param name="filename">上传文件名</param> /// <param name="offSet">偏移</param> /// <param name="intoBuffer">每次上传字节数组 单位KB</param> /// <returns>上传是否成功</returns> bool Upload(string filename, long offSet,153); background-color:inherit; Font-weight:bold">byte[] intoBuffer) //指定上传文件夹+文件名(相对路径) string strPath = "./Resources/" + filename; //将相对路径转换成服务器的绝对路径 strPath = Server.MapPath(strPath); if (offSet < 0) { offSet = 0; } byte[] buffer = intoBuffer; if (buffer != null) { //读写文件的文件流,支持同步读写也支持异步读写 fileStream filesstream = new fileStream(strPath, fileMode.OpenorCreate, fileAccess.ReaDWrite); filesstream.Seek(offSet, SeekOrigin.Begin); filesstream.Write(buffer, 0, buffer.Length); filesstream.Flush(); filesstream.Close(); filesstream.dispose(); return true; false; } } }
客户端代码,后台:
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( 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( (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> 总结
以上是内存溢出为你收集整理的Silverlight通过WebService上传大文件(可拓展为支持断点续传)全部内容,希望文章能够帮你解决Silverlight通过WebService上传大文件(可拓展为支持断点续传)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)