利用WebService实现远程服务器文件的上传和下载
生活随笔
收集整理的這篇文章主要介紹了
利用WebService实现远程服务器文件的上传和下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有時候我們通常需要把上傳的圖片或其他文件放在其他服務器上,以便和網站服務器分開,這時候ASP.NET的WebService就派上大用場 了。我們可以在文件服務器上運行一個WebService,提供上傳和下載文件的方法,然后在網站服務器上的網站中引用這個WebService,調用上 傳或下載的方法,實現對遠程服務器上的文件上傳和下載的功能。以下操作在VS2005環境中完成。
??????先新建一個 WebService項目UpDownFile,里面自動建了一個叫Service1的asmx和cs文件,看不順眼這個名字的就把它重命名吧,我就把它 重命名為了UpDownFile.asmx,把里面的class也重命名為UpDownFile了,這樣看起來順眼多了。
UpDownFile.asmx.cs的代碼如下: usingSystem;usingSystem.Data;usingSystem.Web;usingSystem.Collections;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.ComponentModel;usingSystem.IO;namespaceUpDownFile{????///?<summary>????///?UpDownFile?的摘要說明????///?</summary>????[WebService(Namespace?=?"http://tempuri.org/")]????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]????[ToolboxItem(false)]????public?class?UpDownFile?:?System.Web.Services.WebService????{????????//將Stream流轉換為byte數組的方法。????????//PS:原本想把這個方法也當做WebMethod的,因為客戶端在上傳文件時也要調用該方法,后來發現Stream類型的不能通過WebService傳輸。。。:(????????public?byte[]?ConvertStreamToByteBuffer(Stream?s)????????{????????????MemoryStream?ms?=?new?MemoryStream();????????????int?b;????????????while?((b?=?s.ReadByte())?!=?-1)????????????{????????????????ms.WriteByte((byte)b);????????????}????????????return?ms.ToArray();????????}????????//上傳文件至WebService所在服務器的方法,這里為了操作方法,文件都保存在UpDownFile服務所在文件夾下的File目錄中????????[WebMethod]????????public?bool?Up(byte[]?data,?string?filename)????????{????????????try????????????{????????????????FileStream?fs?=?File.Create(Server.MapPath("File/")?+?filename);????????????????fs.Write(data,?0,?data.Length);????????????????fs.Close();????????????????return?true;????????????}????????????catch????????????{????????????????return?false;????????????}????????}????????//下載WebService所在服務器上的文件的方法????????[WebMethod]????????public?byte[]?Down(string?filename)????????{????????????string?filepath?=?Server.MapPath("File/")?+?filename;????????????if?(File.Exists(filepath))????????????{????????????????try????????????????{????????????????????FileStream?s?=?File.OpenRead(filepath);????????????????????return?ConvertStreamToByteBuffer(s);????????????????}????????????????catch????????????????{????????????????????return?new?byte[0];????????????????}????????????}????????????else????????????{????????????????return?new?byte[0];????????????}????????}????}}
接下來就是在客戶端網站中調用了,先添加Web引用,干脆引用名也用UpDownFile算了,首先是DownFile.aspx usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.IO;namespaceHelloWorld{????public?partial?class?DownFile?:?System.Web.UI.Page????{????????protected?void?Page_Load(object?sender,?EventArgs?e)????????{????????????UpDownFile.UpDownFile?down?=?new?UpDownFile.UpDownFile();????????????byte[]?file?=?down.Down(Request.QueryString["filename"].ToString());????????????Response.BinaryWrite(file);????????}????}}
接下來是上傳的演示文件UpFile.aspx usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.IO;namespaceHelloWorld{????public?partial?class?UpFile?:?System.Web.UI.Page????{????????protected?void?Page_Load(object?sender,?EventArgs?e)????????{????????????????????}????????protected?void?Button1_Click(object?sender,?EventArgs?e)????????{????????????//保存到遠程File文件夾????????????//FileUpload1是aspx頁面的一個FileUpload控件????????????UpDownFile.UpDownFile?up?=?new?UpDownFile.UpDownFile();????????????up.Up(ConvertStreamToByteBuffer(FileUpload1.PostedFile.InputStream),?FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\")?+?1));????????}????????protected?byte[]?ConvertStreamToByteBuffer(Stream?s)????????{????????????MemoryStream?ms?=?new?MemoryStream();????????????int?b;????????????while?((b?=?s.ReadByte())?!=?-1)????????????{????????????????ms.WriteByte((byte)b);????????????}????????????return?ms.ToArray();????????}????}}
??????先新建一個 WebService項目UpDownFile,里面自動建了一個叫Service1的asmx和cs文件,看不順眼這個名字的就把它重命名吧,我就把它 重命名為了UpDownFile.asmx,把里面的class也重命名為UpDownFile了,這樣看起來順眼多了。
UpDownFile.asmx.cs的代碼如下: usingSystem;usingSystem.Data;usingSystem.Web;usingSystem.Collections;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.ComponentModel;usingSystem.IO;namespaceUpDownFile{????///?<summary>????///?UpDownFile?的摘要說明????///?</summary>????[WebService(Namespace?=?"http://tempuri.org/")]????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]????[ToolboxItem(false)]????public?class?UpDownFile?:?System.Web.Services.WebService????{????????//將Stream流轉換為byte數組的方法。????????//PS:原本想把這個方法也當做WebMethod的,因為客戶端在上傳文件時也要調用該方法,后來發現Stream類型的不能通過WebService傳輸。。。:(????????public?byte[]?ConvertStreamToByteBuffer(Stream?s)????????{????????????MemoryStream?ms?=?new?MemoryStream();????????????int?b;????????????while?((b?=?s.ReadByte())?!=?-1)????????????{????????????????ms.WriteByte((byte)b);????????????}????????????return?ms.ToArray();????????}????????//上傳文件至WebService所在服務器的方法,這里為了操作方法,文件都保存在UpDownFile服務所在文件夾下的File目錄中????????[WebMethod]????????public?bool?Up(byte[]?data,?string?filename)????????{????????????try????????????{????????????????FileStream?fs?=?File.Create(Server.MapPath("File/")?+?filename);????????????????fs.Write(data,?0,?data.Length);????????????????fs.Close();????????????????return?true;????????????}????????????catch????????????{????????????????return?false;????????????}????????}????????//下載WebService所在服務器上的文件的方法????????[WebMethod]????????public?byte[]?Down(string?filename)????????{????????????string?filepath?=?Server.MapPath("File/")?+?filename;????????????if?(File.Exists(filepath))????????????{????????????????try????????????????{????????????????????FileStream?s?=?File.OpenRead(filepath);????????????????????return?ConvertStreamToByteBuffer(s);????????????????}????????????????catch????????????????{????????????????????return?new?byte[0];????????????????}????????????}????????????else????????????{????????????????return?new?byte[0];????????????}????????}????}}
接下來就是在客戶端網站中調用了,先添加Web引用,干脆引用名也用UpDownFile算了,首先是DownFile.aspx usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.IO;namespaceHelloWorld{????public?partial?class?DownFile?:?System.Web.UI.Page????{????????protected?void?Page_Load(object?sender,?EventArgs?e)????????{????????????UpDownFile.UpDownFile?down?=?new?UpDownFile.UpDownFile();????????????byte[]?file?=?down.Down(Request.QueryString["filename"].ToString());????????????Response.BinaryWrite(file);????????}????}}
接下來是上傳的演示文件UpFile.aspx usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.IO;namespaceHelloWorld{????public?partial?class?UpFile?:?System.Web.UI.Page????{????????protected?void?Page_Load(object?sender,?EventArgs?e)????????{????????????????????}????????protected?void?Button1_Click(object?sender,?EventArgs?e)????????{????????????//保存到遠程File文件夾????????????//FileUpload1是aspx頁面的一個FileUpload控件????????????UpDownFile.UpDownFile?up?=?new?UpDownFile.UpDownFile();????????????up.Up(ConvertStreamToByteBuffer(FileUpload1.PostedFile.InputStream),?FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\")?+?1));????????}????????protected?byte[]?ConvertStreamToByteBuffer(Stream?s)????????{????????????MemoryStream?ms?=?new?MemoryStream();????????????int?b;????????????while?((b?=?s.ReadByte())?!=?-1)????????????{????????????????ms.WriteByte((byte)b);????????????}????????????return?ms.ToArray();????????}????}}
其實主要是UpDownFile.asmx.cs這個Web服務文件,客戶端怎么調用就隨便了。大家看到了,Up和Down方法中傳輸的都是byte[]數組,因為WebService不支持Stream傳輸,那就把文件轉換為二進制數組來傳遞。
??????此 外也可以制作利用WebService遠程存儲數據庫的東西,因為有時候遠程服務器的1433端口不能開放,只能開放80,那WebService就派上 用場了。要注意的是,DataTable不能通過WebService傳輸,只能附在DataSet上來傳輸,此外很多類型也不可以通過 WebService傳,具體可以通過其傳輸的類型請參考書籍
總結
以上是生活随笔為你收集整理的利用WebService实现远程服务器文件的上传和下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022四川水利职业技术学院专业排名(哪
- 下一篇: ASP.NET MVC中在Action获