日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用WebService实现远程服务器文件的上传和下载

發布時間:2024/9/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用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();????????}????}}


其實主要是UpDownFile.asmx.cs這個Web服務文件,客戶端怎么調用就隨便了。大家看到了,Up和Down方法中傳輸的都是byte[]數組,因為WebService不支持Stream傳輸,那就把文件轉換為二進制數組來傳遞。
??????此 外也可以制作利用WebService遠程存儲數據庫的東西,因為有時候遠程服務器的1433端口不能開放,只能開放80,那WebService就派上 用場了。要注意的是,DataTable不能通過WebService傳輸,只能附在DataSet上來傳輸,此外很多類型也不可以通過 WebService傳,具體可以通過其傳輸的類型請參考書籍

。我想遠程上下傳文件和遠程存取數據庫是WebService最主要的應用吧? 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的利用WebService实现远程服务器文件的上传和下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。