国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net Web Services上傳和下載文件(完整代碼)第1/2頁

2020-01-18 01:26:39
字體:
供稿:網(wǎng)友
下面,我們就分別介紹如何通過Web Services從服務器下載文件到客戶端和從客戶端通過Web Services上載文件到服務器。
一:通過Web Services顯示和下載文件
我們這里建立的Web Services的名稱為GetBinaryFile,提供兩個公共方法:分別是GetImage()和GetImageType(),前者返回二進制文件字節(jié)數(shù)組,后者返回文件類型,其中,GetImage()方法有一個參數(shù),用來在客戶端選擇要顯示或下載的文件名字。這里我們所顯示和下載的文件可以不在虛擬目錄下,采用這個方法的好處是:可以根據(jù)權(quán)限對文件進行顯示和下載控制,從下面的方法我們可以看出,實際的文件位置并沒有在虛擬目錄下,因此可以更好地對文件進行權(quán)限控制,這在對安全性有比較高的情況下特別有用。這個功能在以前的ASP程序中可以用Stream對象實現(xiàn)。為了方便讀者進行測試,這里列出了全部的源代碼,并在源代碼里進行介紹和注釋。
首先,建立GetBinaryFile.asmx文件:
我們可以在VS.NET里新建一個C#的aspxWebCS工程,然后“添加新項”,選擇“Web服務”,并設定文件名為:GetBinaryFile.asmx,在“查看代碼”中輸入以下代碼,即:GetBinaryFile.asmx.cs:
復制代碼 代碼如下:

usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Diagnostics;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.Services;
usingSystem.IO;
namespacexml.sz.luohuedu.net.aspxWebCS
{
///<summary>
///GetBinaryFile的摘要說明。
///WebServices名稱:GetBinaryFile
///功能:返回服務器上的一個文件對象的二進制字節(jié)數(shù)組。
///</summary>
[WebService(Namespace="http://xml.sz.luohuedu.net/",
Description="在WebServices里利用.NET框架進行傳遞二進制文件。")]
publicclassGetBinaryFile:System.Web.Services.WebService
{
#regionComponentDesignergeneratedcode
//Web服務設計器所必需的
privateIContainercomponents=null;
///<summary>
///清理所有正在使用的資源。
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&components!=null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
publicclassImages:System.Web.Services.WebService
{
///<summary>
///Web服務提供的方法,返回給定文件的字節(jié)數(shù)組。
///</summary>
[WebMethod(Description="Web服務提供的方法,返回給定文件的字節(jié)數(shù)組")]
publicbyte[]GetImage(stringrequestFileName)
{
///得到服務器端的一個圖片
///如果你自己測試,注意修改下面的實際物理路徑
if(requestFileName==null||requestFileName=="")
returngetBinaryFile("D://Picture.JPG");
else
returngetBinaryFile("D://"+requestFileName);
}
///<summary>
///getBinaryFile:返回所給文件路徑的字節(jié)數(shù)組。
///</summary>
///<paramname="filename"></param>
///<returns></returns>
publicbyte[]getBinaryFile(stringfilename)
{
if(File.Exists(filename))
{
try
{
///打開現(xiàn)有文件以進行讀取。
FileStreams=File.OpenRead(filename);
returnConvertStreamToByteBuffer(s);
}
catch(Exceptione)
{
returnnewbyte[0];
}
}
else
{
returnnewbyte[0];
}
}
///<summary>
///ConvertStreamToByteBuffer:把給定的文件流轉(zhuǎn)換為二進制字節(jié)數(shù)組。
///</summary>
///<paramname="theStream"></param>
///<returns></returns>
publicbyte[]ConvertStreamToByteBuffer(System.IO.StreamtheStream)
{
intb1;
System.IO.MemoryStreamtempStream=newSystem.IO.MemoryStream();
while((b1=theStream.ReadByte())!=-1)
{
tempStream.WriteByte(((byte)b1));
}
returntempStream.ToArray();
}
[WebMethod(Description="Web服務提供的方法,返回給定文件類型。")]
publicstringGetImageType()
{
///這里只是測試,您可以根據(jù)實際的文件類型進行動態(tài)輸出
return"image/jpg";
}
}
}
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洛扎县| 武乡县| 贺州市| 无棣县| 游戏| 长宁县| 化德县| 明溪县| 仙游县| 宽甸| 含山县| 简阳市| 玉山县| 寿光市| 崇仁县| 台江县| 罗平县| 二连浩特市| 陆川县| 镇巴县| 桂阳县| 哈尔滨市| 南昌市| 新晃| 铁力市| 天水市| 昌邑市| 余干县| 朝阳市| 安宁市| 玉屏| 广饶县| 吉隆县| 龙陵县| 益阳市| 梧州市| 来安县| 黄大仙区| 晋宁县| 尚义县| 景洪市|