欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net下實現(xiàn)支持文件分塊多點異步上傳的 Web Services

 更新時間:2007年04月13日 00:00:00   作者:  
本文的客戶端應用程序不包括 ASP.Net Web 應用程序!

本文假設 URL: http://localhost/mywebservices/updownload.asmx

共有 4 個程序文件 (Web.Config 就不贅述了)

Server Side: 

標題中所提到的 "異步" 其實在服務器端的程序并沒有什么特殊的,而主要是通過客戶端應用程序
異步調(diào)用相關 Web Method 實現(xiàn)的!

1. updownload.asmx ,位于 IIS 的某個 Web 共享目錄,代碼如下,只有一句話:

<%@ WebService Language="c#" Codebehind="UpDownLoad.asmx.cs" Class="Service1" %>

2. updownload.asmx.cs ,即: updownload.asmx 的 Codebehind ,位于 IIS 的某個 Web 共享目錄的 bin 子目錄下,代碼如下:

/*

本文件位于 Web 共享目錄的 bin 子目錄下,通過執(zhí)行如下命令行編譯:
csc /t:library updownload.asmx.cs

*/
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;
using System;

public class Service1 : System.Web.Services.WebService
{
 [WebMethod]
 public string HelloWorld()
 {
  return "Hello World";
 }

 //從 Web Method 本身,其實看不出 "同步" 還是 "異步"
 [WebMethod(Description = "為了支持多點分塊異步上傳文件,此方法必須由客戶端預先調(diào)用,以便在服務器端生成指定 FileName 和 Length 大小的空白文件預定空間! 建議客戶端同步調(diào)用")]
 public string CreateBlankFile(string FileName,int Length) //建議由客戶端同步調(diào)用
 {
  FileStream fs = new FileStream(Server.MapPath(".") + "\\" + FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
  fs.Write(new byte[Length], 0, Length);
  fs.Close();
  fs = null;
  return FileName + " (" + Length + ") 空白文件已經(jīng)創(chuàng)建!";
 }

 [WebMethod(Description = "提供一個用于一次完整上傳整個文件的方法! 建議客戶端同步調(diào)用")]
 public string UploadFileBytes(byte[] Bytes,string FileName)
 {
  return UploadFileChunkBytes(Bytes, 0, FileName);
 }

 [WebMethod(Description = "提供一個用于一次只上傳由 Position 位置起始的, Bytes 字節(jié)的 FileName 文件塊存入服務器端相應文件的相應字節(jié)位置! 建議客戶端異步調(diào)用")]
 // 這里只要多提供一個 Position 參數(shù),余下的再由客戶端調(diào)用異步的該方法,就輕松達到目的了!
 public string UploadFileChunkBytes(byte[] Bytes,int Position,string FileName)
 {
  try
  {
   FileStream fs = new FileStream(Server.MapPath(".") + "\\" + FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
   //該 Bytes 的字節(jié)要寫到 服務器端 相應文件的從 Position 開始的字節(jié)
   fs.Position = Position;
   fs.Write(Bytes, 0, Bytes.Length);
   fs.Close();
   fs = null;
   return FileName + " 文件塊: 位置[" + Position + "," + (Position + Bytes.Length) + "] 大小(" + Bytes.Length + ") 上傳成功!";
  }
  catch (Exception e)
  {
   return e.Message;
  }
 }

 [WebMethod]
 public byte[] DownloadFileBytes(string FileName)
 {
  if (File.Exists(FileName))
  {
   try
   {
    FileStream fs = File.OpenRead(FileName);
    int i = (int) fs.Length;
    byte[] ba = new byte[i];
    fs.Read(ba,0,i);
    fs.Close();
    return ba;
   }
   catch
   {
    return new byte[0];
   }
  }
  else
  {
   return new byte[0];
  }
 }
}


//=======================================================================

Client Side:
3. UpDownloadProxy.cs :
 本文件由如下命令生成
 % Visual Studio .Net 2003 安裝目錄下的 %\SDK\v1.1\Bin\wsdl.exe
 具體命令行如下:
 wsdl.exe /l:CS /out:UpDownloadProxy.cs http://localhost/MyWebServices/updownload.asmx?wsdl
 生成的本地的客戶端代理類代碼里已經(jīng)為每個 Web Method 生成了可異步和同步執(zhí)行的方法,例如:
    public string HelloWorld() {}
    public System.IAsyncResult BeginHelloWorld(...) {}
    public string EndHelloWorld(...) {}

 下面是該命令行生成的完整的 UpDownloadProxy.cs 代碼,就不修改了:
/*

通過執(zhí)行如下命令行編譯,生成 UpDownloadProxy.dll :
csc /t:library UpDownloadProxy.cs

*/

//------------------------------------------------------------------------------
// <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);
    }

    /// <remarks/>
    public string EndHelloWorld(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/CreateBlankFile", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public string CreateBlankFile(string FileName, int Length) {
        object[] results = this.Invoke("CreateBlankFile", new object[] {
                    FileName,
                    Length});
        return ((string)(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BeginCreateBlankFile(string FileName, int Length, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("CreateBlankFile", new object[] {
                    FileName,
                    Length}, callback, asyncState);
    }

    /// <remarks/>
    public string EndCreateBlankFile(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/UploadFileBytes", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public string UploadFileBytes([System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")] System.Byte[] Bytes, string FileName) {
        object[] results = this.Invoke("UploadFileBytes", new object[] {
                    Bytes,
                    FileName});
        return ((string)(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BeginUploadFileBytes(System.Byte[] Bytes, string FileName, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("UploadFileBytes", new object[] {
                    Bytes,
                    FileName}, callback, asyncState);
    }

    /// <remarks/>
    public string EndUploadFileBytes(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/UploadFileChunkBytes", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public string UploadFileChunkBytes([System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")] System.Byte[] Bytes, int Position, string FileName) {
        object[] results = this.Invoke("UploadFileChunkBytes", new object[] {
                    Bytes,
                    Position,
                    FileName});
        return ((string)(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BeginUploadFileChunkBytes(System.Byte[] Bytes, int Position, string FileName, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("UploadFileChunkBytes", new object[] {
                    Bytes,
                    Position,
                    FileName}, callback, asyncState);
    }

    /// <remarks/>
    public string EndUploadFileChunkBytes(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/DownloadFileBytes", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")]
    public System.Byte[] DownloadFileBytes(string FileName) {
        object[] results = this.Invoke("DownloadFileBytes", new object[] {
                    FileName});
        return ((System.Byte[])(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BeginDownloadFileBytes(string FileName, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("DownloadFileBytes", new object[] {
                    FileName}, callback, asyncState);
    }

    /// <remarks/>
    public System.Byte[] EndDownloadFileBytes(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((System.Byte[])(results[0]));
    }
}

//=======================================================================
4. UpDownloadClient.cs :
 該程序才是真正實現(xiàn)文件分塊多點異步上傳的核心代碼:

/*

通過執(zhí)行如下命令行編譯:
csc updownloadClient.cs /r:updownloadproxy.dll

*/
using System;
using System.IO;

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);
  //調(diào)用 "同步執(zhí)行" 的本地 Web Sevices 代理類的 方法,相當于同步調(diào)用了 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; //用于記錄剩余還未上傳的字節(jié)數(shù),初值是文件的大小

  //調(diào)用 "同步執(zhí)行" 的本地 Web Sevices 代理類的 方法,相當于同步調(diào)用了 Web Method !
  //預定服務器端空間
  xx.CreateBlankFile(filename,r);
  int size = ChunkSize * 1024;
  int k = 0; //用于記錄已經(jīng)上傳的字節(jié)數(shù)
  i++; //用于記錄上傳的文件塊數(shù)
  while (r >= size)
  {
   byte[] buffer = new byte[size];
   fs.Read(buffer,0,buffer.Length);
   //調(diào)用 "異步執(zhí)行" 的本地 Web Sevices 代理類的 方法,相當于異步調(diào)用了 Web Method !
   //該 buffer 的字節(jié)要寫到 服務器端 相應文件的從 Position = k 開始的字節(jié)
   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);
   //調(diào)用 "異步執(zhí)行" 的本地 Web Sevices 代理類的 方法,相當于異步調(diào)用了 Web Method !
   //該 buffer 的字節(jié)要寫到 服務器端 相應文件的從 Position = k 開始的字節(jié)
   xx.BeginUploadFileChunkBytes(buffer,k,filename,new AsyncCallback(UploadFileChunkCallback),xx);
   i++;
  }
  fs.Close();

 }

 private static int i = -1; //用于記錄上傳的文件塊數(shù)

 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 上傳文件非常簡單,甚至比傳統(tǒng)的 http Web 上傳還簡單!
同時較輕松地就實現(xiàn)了文件分塊多點異步上傳:
Server 端代碼沒啥特殊的!
Client 端代碼稍微復雜些!

相關文章

  • 使用pdfbox實現(xiàn)pdf文本提取和合并功能示例

    使用pdfbox實現(xiàn)pdf文本提取和合并功能示例

    這篇文章主要介紹了使用pdfbox實現(xiàn)pdf文本提取和合并功能示例,大家參考使用吧
    2014-01-01
  • asp.net mvc 實現(xiàn)文件上傳帶進度條的思路與方法

    asp.net mvc 實現(xiàn)文件上傳帶進度條的思路與方法

    這篇文章主要給大家介紹了關于asp.net mvc 實現(xiàn)文件上傳帶進度條的思路與方法,文中給出了詳細的示例代碼,相信對大家的理解和學習很有幫助,有需要的朋友們可以參考借鑒,下面來跟著小編一起學習學習吧。
    2016-12-12
  • Request.UrlReferrer中文亂碼解決方法

    Request.UrlReferrer中文亂碼解決方法

    參考了網(wǎng)絡大部分的解決方案,沒一個能搞定的,如果窮途末路,試試下面的方法:將獲得的前一頁面的URL分成兩段,后面的參數(shù)部分進行編碼(直接對URL編碼是不行的),然后再組合一下就可以了,需要的朋友可以了解下
    2012-12-12
  • Nginx配置實現(xiàn)下載文件的示例代碼

    Nginx配置實現(xiàn)下載文件的示例代碼

    這篇文章主要介紹了Nginx配置實現(xiàn)下載文件的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • .NET中如何將文本文件的內(nèi)容存儲到DataSet

    .NET中如何將文本文件的內(nèi)容存儲到DataSet

    大家在項目中比較多的會對文件進行操作,例如文件的上傳下載,文件的壓縮和解壓等IO操作。而在.NET項目中較多的會使用DataSet,DataTable進行數(shù)據(jù)的緩存。每一個DataSet都是一個或多個DataTable對象的集合,本文主要介紹的是如何將文本文件的內(nèi)容存儲到DataSet里去。
    2016-12-12
  • c#實現(xiàn)根據(jù)網(wǎng)絡IP顯示地理位置功能示例

    c#實現(xiàn)根據(jù)網(wǎng)絡IP顯示地理位置功能示例

    通常都會有類似 注冊IP和最后登錄IP這兩個的字段來存儲用戶注冊時候的IP地址和最后登錄的IP的地址,現(xiàn)在我們就簡單的實現(xiàn)一下如標題所示的功能
    2013-06-06
  • asp.net 用戶在線退出更新實現(xiàn)代碼

    asp.net 用戶在線退出更新實現(xiàn)代碼

    更新用戶是否在線?注銷用戶的話有三種情況:1.點擊退出,2.會話超時,3.關閉瀏覽器
    2010-03-03
  • asp.net core集成MongoDB的完整步驟

    asp.net core集成MongoDB的完整步驟

    前兩天在學習MongoDB相關的知識,做了個小Demo,下面這篇文章主要給大家介紹了關于asp.net core集成MongoDB的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-10-10
  • 控件開發(fā)時兩種JS嵌入資源方式的使用方法

    控件開發(fā)時兩種JS嵌入資源方式的使用方法

    控件開發(fā)時兩種JS嵌入資源方式的使用方法...
    2007-04-04
  • .Net?Core基于ImageSharp實現(xiàn)圖片縮放與裁剪

    .Net?Core基于ImageSharp實現(xiàn)圖片縮放與裁剪

    這篇文章介紹了.Net?Core基于ImageSharp實現(xiàn)圖片縮放與裁剪的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06

最新評論