隨著Internet技術的發展和跨平台需求的日益增加,Web Services的應用越來越廣,我們不但需要通過Web Services傳遞字串資訊,而且需要傳遞二進位檔案資訊。下面,我們就分別介紹如何通過Web Services從伺服器下載檔案到用戶端和從用戶端通過Web Services上傳檔案到伺服器。
一:通過Web Services顯示和下載檔案
我們這裡建立的Web Services的名稱為GetBinaryFile,提供兩個公用方法:分別是GetImage()和GetImageType(),前者返回二進位檔案位元組數組,後者返迴文件類型,其中,GetImage()方法有一個參數,用來在用戶端選擇要顯示或下載的檔案名稱字。這裡我們所顯示和下載的檔案可以不在虛擬目錄下,採用這個方法的好處是:可以根據許可權對檔案進行顯示和下載控制,從下面的方法我們可以看出,實際的檔案位置並沒有在虛擬目錄下,因此可以更好地對檔案進行許可權控制,這在對安全性有比較高的情況下特別有用。這個功能在以前的ASP程式中可以用Stream對象實現。為了方便讀者進行測試,這裡列出了全部的原始碼,並在原始碼裡進行介紹和注釋。
首先,建立GetBinaryFile.asmx檔案:
我們可以在VS.NET裡建立一個C#的aspxWebCS工程,然後“添加新項”,選擇“Web服務”,並設定檔案名稱為:GetBinaryFile.asmx,在“查看代碼”中輸入以下代碼,即:GetBinaryFile.asmx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.Services;
using System.IO;
namespace xml.sz.luohuedu.net.aspxWebCS
{
/// <summary>
/// GetBinaryFile 的摘要說明。
/// Web Services名稱:GetBinaryFile
/// 功能:返回伺服器上的一個檔案對象的二進位位元組數組。
/// </summary>
[WebService(Namespace="http://xml.sz.luohuedu.net/",
Description="在Web Services裡利用.NET架構進行傳遞二進位檔案。")]
public class GetBinaryFile : System.Web.Services.WebService
{
#region Component Designer generated code
//Web 服務設計器所必需的
private IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
public class Images: System.Web.Services.WebService
{
/// <summary>
/// Web 服務提供的方法,返回給定檔案的位元組數組。
/// </summary>
[WebMethod(Description="Web 服務提供的方法,返回給定檔案的位元組數組")]
public byte[] GetImage(string requestFileName)
{
///得到伺服器端的一個圖片
///如果你自己測試,注意修改下面的實際實體路徑
if(requestFileName == null || requestFileName == "")
return getBinaryFile("D:\\Picture.JPG");
else
return getBinaryFile("D:\\" + requestFileName);
}
/// <summary>
/// getBinaryFile:返回所給檔案路徑的位元組數組。
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public byte[] getBinaryFile(string filename)
{
if(File.Exists(filename))
{
try
{
///開啟現有檔案以進行讀取。
FileStream s = File.OpenRead(filename);
return ConvertStreamToByteBuffer(s);
}
catch(Exception e)
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}
/// <summary>
/// ConvertStreamToByteBuffer:把給定的檔案流轉換為二進位位元組數組。
/// </summary>
/// <param name="theStream"></param>
/// <returns></returns>
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while((b1=theStream.ReadByte())!=-1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
[WebMethod(Description="Web 服務提供的方法,返回給定檔案類型。")]
public string GetImageType()
{
///這裡只是測試,您可以根據實際的檔案類型進行動態輸出
return "image/jpg";
}
}
}
}
一旦我們建立了上面的asmx檔案,進行編譯後,我們就可以編寫用戶端的代碼來進行調用這個Web Services了。
我們先“添加Web引用”,輸入:http://localhost/aspxWebCS/GetBinaryFile.asmx。下面,我們編寫顯示檔案的中間檔案:GetBinaryFileShow.aspx,這裡,我們只需要在後代碼裡編寫代碼即可,GetBinaryFileShow.aspx.cs檔案內容如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Services;
namespace aspxWebCS
{
/// <summary>
/// GetBinaryFileShow 的摘要說明。
/// </summary>
public class GetBinaryFileShow : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
///定義並初始設定檔案對象;
xml.sz.luohuedu.net.aspxWebCS.GetBinaryFile.Images oImage;
oImage = new xml.sz.luohuedu.net.aspxWebCS.GetBinaryFile.Images();
///得到二進位檔案位元組數組;
byte[] image = oImage.GetImage("");
///轉換為支援儲存區為記憶體的流
System.IO.MemoryStream memStream = new System.IO.MemoryStream(image);
///定義並執行個體化Bitmap對象
Bitmap bm = new Bitmap(memStream);
///根據不同的條件進行輸出或者下載;
Response.Clear();
///如果請求字串指定下載,就下載該檔案;
///否則,就顯示在瀏覽器中。
if(Request.QueryString["Download"]=="1")
{
Response.Buffer = true;
&n