//------------------------------------------------------------------------------ // <autogenerated> // This code was generated by a tool. // Runtime Version: 1.1.4322.573 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </autogenerated> //------------------------------------------------------------------------------
// // 此源代码由 wsdl, Version=1.1.4322.573 自动生成。 // using System.Diagnostics; using System.Xml.Serialization; using System; using System.Web.Services.Protocols; using System.ComponentModel; using System.Web.Services;
/// <remarks/> [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Web.Services.WebServiceBindingAttribute(Name="Service1Soap", Namespace="http://tempuri.org/")] public class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol {
/// <remarks/> public Service1() { this.Url = "http://localhost/MyWebServices/updownload.asmx"; }
/// <remarks/> [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public string HelloWorld() { object[] results = this.Invoke("HelloWorld", new object[0]); return ((string)(results[0])); }
/// <remarks/> public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("HelloWorld", new object[0], callback, asyncState); }
public class Class1 { static void Main(string[] args) { //Download(ServerSidepath, ClientSidePath) Download(@"e:\test.jpg", @"f:\test_local.jpg"); System.Console.WriteLine("down End");
System.Console.WriteLine("同步 up file exec ..."); UploadFile(@"e:\Northwind.mdb"); System.Console.WriteLine("同步 up file End\n");
System.Console.WriteLine("异步 up chunks exec ..."); UploadFileChunks(@"e:\test.rar", 64); System.Console.ReadLine(); }
public static void UploadFile(string LocalFileName) { Service1 xx = new Service1(); FileStream fs = new FileStream(LocalFileName, FileMode.Open); //Client Side Path byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); //调用 "同步执行" 的本地 Web Sevices 代理类的 方法,相当于同步调用了 Web Method ! xx.UploadFileBytes(buffer, System.IO.Path.GetFileName(LocalFileName)); }
//指定要上传的本地文件的路径,及每次上传文件块的大小 public static void UploadFileChunks(string LocalFileName,int ChunkSize) { Service1 xx = new Service1(); string filename = System.IO.Path.GetFileName(LocalFileName);
FileStream fs = new FileStream(LocalFileName, FileMode.Open); //Client Side Path //fs = File.OpenRead(LocalFileName);
int r = (int) fs.Length; //用于记录剩余还未上传的字节数,初值是文件的大小
//调用 "同步执行" 的本地 Web Sevices 代理类的 方法,相当于同步调用了 Web Method ! //预定服务器端空间 xx.CreateBlankFile(filename,r); int size = ChunkSize * 1024; int k = 0; //用于记录已经上传的字节数 i++; //用于记录上传的文件块数 while (r >= size) { byte[] buffer = new byte[size]; fs.Read(buffer,0,buffer.Length); //调用 "异步执行" 的本地 Web Sevices 代理类的 方法,相当于异步调用了 Web Method ! //该 buffer 的字节要写到 服务器端 相应文件的从 Position = k 开始的字节 xx.BeginUploadFileChunkBytes(buffer,k,filename,new AsyncCallback(UploadFileChunkCallback),xx); k += size; r -= size; i++; } if (r > 0) //剩余的零头 { byte[] buffer = new byte[r]; fs.Read(buffer,0,buffer.Length); //调用 "异步执行" 的本地 Web Sevices 代理类的 方法,相当于异步调用了 Web Method ! //该 buffer 的字节要写到 服务器端 相应文件的从 Position = k 开始的字节 xx.BeginUploadFileChunkBytes(buffer,k,filename,new AsyncCallback(UploadFileChunkCallback),xx); i++; } fs.Close();
}
private static int i = -1; //用于记录上传的文件块数
private static void UploadFileChunkCallback(IAsyncResult ar) { Service1 x = (Service1) ar.AsyncState; Console.WriteLine(x.EndUploadFileChunkBytes(ar)); if ( --i == 0) { Console.WriteLine("异步 up all chunks end"); } }
public static void Download(string ServerSideFileName,string LocalFileName) { Service1 xx = new Service1(); byte[] ba = xx.DownloadFileBytes(ServerSideFileName); //Server Side Path
FileStream fs = new FileStream(LocalFileName, FileMode.Create); //Client Side Path fs.Write(ba,0,ba.Length); fs.Close(); } }
//=========================================================================== 至此我们通过纯手工的方式完成了任务,之所以不用 VS 就是为了让码子简洁明了! Microshaoft .Night 就是这么平易近人! (PMPMP to MS) 通过 Web Sevices 上传文件非常简单,甚至比传统的 http Web 上传还简单! 同时较轻松地就实现了文件分块多点异步上传: Server 端代码没啥特殊的! Client 端代码稍微复杂些!