一:通過Web Services顯示和下載文件
我們這里建立的Web Services的名稱為GetBinaryFile,提供兩個(gè)公共方法:分別是GetImage()和GetImageType(),前者返回二進(jìn)制文件字節(jié)數(shù)組,后者返回文件類型,其中,GetImage()方法有一個(gè)參數(shù),用來在客戶端選擇要顯示或下載的文件名字。這里我們所顯示和下載的文件可以不在虛擬目錄下,采用這個(gè)方法的好處是:可以根據(jù)權(quán)限對(duì)文件進(jìn)行顯示和下載控制,從下面的方法我們可以看出,實(shí)際的文件位置并沒有在虛擬目錄下,因此可以更好地對(duì)文件進(jìn)行權(quán)限控制,這在對(duì)安全性有比較高的情況下特別有用。這個(gè)功能在以前的ASP程序中可以用Stream對(duì)象實(shí)現(xiàn)。為了方便讀者進(jìn)行測試,這里列出了全部的源代碼,并在源代碼里進(jìn)行介紹和注釋。
首先,建立GetBinaryFile.asmx文件:
我們可以在VS.NET里新建一個(gè)C#的aspxWebCS工程,然后“添加新項(xiàng)”,選擇“Web服務(wù)”,并設(shè)定文件名為: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
/// 功能:返回服務(wù)器上的一個(gè)文件對(duì)象的二進(jìn)制字節(jié)數(shù)組。
///
</summary>
[WebService(Namespace="http://xml.sz.luohuedu.net/",
Description="在Web
Services里利用.NET框架進(jìn)行傳遞二進(jìn)制文件。")]
public class GetBinaryFile :
System.Web.Services.WebService
{
#region Component Designer generated code
//Web
服務(wù)設(shè)計(jì)器所必需的
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 服務(wù)提供的方法,返回給定文件的字節(jié)數(shù)組。
///
</summary>
[WebMethod(Description="Web
服務(wù)提供的方法,返回給定文件的字節(jié)數(shù)組")]
public byte[] GetImage(string
requestFileName)
{
///得到服務(wù)器端的一個(gè)圖片
///如果你自己測試,注意修改下面的實(shí)際物理路徑
if(requestFileName == null || requestFileName
== "")
return getBinaryFile("D:\\Picture.JPG");
else
return getBinaryFile("D:\\" + requestFileName);
}
/// <summary>
/// getBinaryFile:返回所給文件路徑的字節(jié)數(shù)組。
///
</summary>
/// <param name="filename"></param>
/// <returns></returns>
public byte[] getBinaryFile(string
filename)
{
if(File.Exists(filename))
{
try
{
///打開現(xiàn)有文件以進(jìn)行讀取。
FileStream s =
File.OpenRead(filename);
return ConvertStreamToByteBuffer(s);
}
catch(Exception e)
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}
/// <summary>
///
ConvertStreamToByteBuffer:把給定的文件流轉(zhuǎn)換為二進(jìn)制字節(jié)數(shù)組。
/// </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
服務(wù)提供的方法,返回給定文件類型。")]
public string GetImageType()
{
///這里只是測試,您可以根據(jù)實(shí)際的文件類型進(jìn)行動(dòng)態(tài)輸出
return "image/jpg";
}
}
}
}
一旦我們創(chuàng)建了上面的asmx文件,進(jìn)行編譯后,我們就可以編寫客戶端的代碼來進(jìn)行調(diào)用這個(gè)Web Services了。
我們先“添加Web引用”,輸入:http://localhost/aspxWebCS/GetBinaryFile.asmx。下面,我們編寫顯示文件的中間文件:GetBinaryFileShow.aspx,這里,我們只需要在后代碼里編寫代碼即可,GetBinaryFileShow.aspx.cs文件內(nèi)容如下:
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)
{
//
在此處放置用戶代碼以初始化頁面
///定義并初始化文件對(duì)象;
xml.sz.luohuedu.net.aspxWebCS.GetBinaryFile.Images oImage;
oImage = new
xml.sz.luohuedu.net.aspxWebCS.GetBinaryFile.Images();
///得到二進(jìn)制文件字節(jié)數(shù)組;
byte[] image = oImage.GetImage("");
///轉(zhuǎn)換為支持存儲(chǔ)區(qū)為內(nèi)存的流
System.IO.MemoryStream memStream = new
System.IO.MemoryStream(image);
///定義并實(shí)例化Bitmap對(duì)象
Bitmap bm = new
Bitmap(memStream);
///根據(jù)不同的條件進(jìn)行輸出或者下載;
Response.Clear();
///如果請(qǐng)求字符串指定下載,就下載該文件;
///否則,就顯示在瀏覽器中。
if(Request.QueryString["Download"]=="1")
{
Response.Buffer =
true;
Response.ContentType = "application/octet-stream";
///這里下載輸出的文件名字 ok.jpg 為例子,你實(shí)際中可以根據(jù)情況動(dòng)態(tài)決定。
Response.AddHeader("Content-Disposition","attachment;filename=ok.jpg");
}
else
Response.ContentType = oImage.GetImageType();
Response.BinaryWrite(image);
Response.End();
}
#region Web Form Designer generated code
override protected void
OnInit(EventArgs e)
{
//
// CODEGEN:該調(diào)用是 ASP.NET Web
窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
///
此方法的內(nèi)容。
/// </summary>
private void
InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion
}
}
最后,我們就編寫最終的瀏覽頁面:GetBinaryFile.aspx,這個(gè)文件很簡單,只需要aspx文件即可,內(nèi)容如下:
<%@ Page language="c#" Codebehind="GetBinaryFile.aspx.cs"
AutoEventWireup="false"
Inherits="aspxWebCS.GetBinaryFile"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<HTML>
<HEAD>
<title>通過Web
Services顯示和下載文件</title>
<meta name="GENERATOR"
Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE"
Content="C#">
<meta name="vs_defaultClientScript"
content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form
id="GetBinaryFile" method="post" runat="server">
<FONT
face="宋體">
<asp:HyperLink id="HyperLink1"
NavigateUrl="GetBinaryFileShow.aspx?Download=1"
runat="server">下載文件</asp:HyperLink>
<br/>
<!--下面是直接顯示文件-->
<asp:Image id="Image1"
ImageUrl="GetBinaryFileShow.aspx" runat="server"></asp:Image>
</FONT>
</form>
</body>
</HTML>
二:通過Web Services上載文件
向服務(wù)器上載文件可能有許多種方法,在利用Web Services上載文件的方法中,下面的這個(gè)方法應(yīng)該是最簡單的了。我們?nèi)韵笄懊娴睦幽菢?,首先建立Upload.asmx文件,其Upload.asmx.cs內(nèi)容如下,里面已經(jīng)做了注釋:
using System;
using System.Collections;
using
System.ComponentModel;
using System.Data;
using
System.Diagnostics;
using System.Web;
using
System.Web.Services;
using System.IO;
namespace xml.sz.luohuedu.net.aspxWebCS
{
///
<summary>
/// Upload 的摘要說明。
/// </summary>
[WebService(Namespace="http://xml.sz.luohuedu.net/",
Description="在Web
Services里利用.NET框架進(jìn)上載文件。")]
public class Upload :
System.Web.Services.WebService
{
public
Upload()
{
//CODEGEN:該調(diào)用是 ASP.NET Web
服務(wù)設(shè)計(jì)器所必需的
InitializeComponent();
}
#region Component Designer generated code
//Web 服務(wù)設(shè)計(jì)器所必需的
private IContainer components = null;
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
///
此方法的內(nèi)容。
/// </summary>
private void
InitializeComponent()
{
}
/// <summary>
/// 清理所有正在使用的資源。
///
</summary>
protected override void Dispose( bool disposing
)
{
if(disposing && components !=
null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
[WebMethod(Description="Web 服務(wù)提供的方法,返回是否文件上載成功與否。")]
public string
UploadFile(byte[] fs,string FileName)
{
try
{
///定義并實(shí)例化一個(gè)內(nèi)存流,以存放提交上來的字節(jié)數(shù)組。
MemoryStream m = new
MemoryStream(fs);
///定義實(shí)際文件對(duì)象,保存上載的文件。
FileStream f = new
FileStream(Server.MapPath(".") + "\\"
+ FileName,
FileMode.Create);
///把內(nèi)內(nèi)存里的數(shù)據(jù)寫入物理文件
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
return
"文件已經(jīng)上傳成功。";
}
catch(Exception ex)
{
return
ex.Message;
}
}
}
}
要上載文件,必須提供一個(gè)表單,來供用戶進(jìn)行文件的選擇,下面我們就建立這樣一個(gè)頁面Upload.aspx,用來提供文件上載:
<%@ Page language="c#" Codebehind="Upload.aspx.cs"
AutoEventWireup="false"
Inherits="aspxWebCS.Upload" %>
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>通過Web Services上載文件</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta
name="vs_defaultClientScript" content="JavaScript">
<meta
name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form
id="Form1" method="post" runat="server"
enctype="multipart/form-data">
<INPUT id="MyFile" type="file"
runat="server">
<br>
<br>
<asp:Button id="Button1" runat="server"
Text="上載文件"></asp:Button>
</form>
</body>
</HTML>
我們要進(jìn)行處理的是在后代碼里面,下面詳細(xì)的介紹,Upload.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;
using System.IO;
namespace aspxWebCS
{
/// <summary>
/// Upload
的摘要說明。
/// 利用該方法通過Web Services上載文件
/// </summary>
public
class Upload : System.Web.UI.Page
{
protected
System.Web.UI.HtmlControls.HtmlInputFile MyFile;
protected
System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs
e)
{
// 在此處放置用戶代碼以初始化頁面
}
#region Web Form Designer generated code
override protected void
OnInit(EventArgs e)
{
//
// CODEGEN:該調(diào)用是 ASP.NET
Web
窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
///
此方法的內(nèi)容。
/// </summary>
private void
InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
///首先得到上載文件信息和文件流
if(MyFile.PostedFile != null)
{
System.Web.HttpFileCollection oFiles;
oFiles =
System.Web.HttpContext.Current.Request.Files;
if(oFiles.Count <
1)
{
Response.Write ("請(qǐng)選擇文件。");
Response.End();
}
string FilePath = oFiles[0].FileName;
if(FilePath == "" ||
FilePath == null)
{
Response.Write ("請(qǐng)選擇一個(gè)文件。");
Response.End();
}
string FileName =
FilePath.Substring(FilePath.LastIndexOf("\\")+1);
try
{
///處理上載的文件流信息。
byte[] b = new byte[oFiles[0].ContentLength];
System.IO.Stream fs;
xml.sz.luohuedu.net.aspxWebCS.Upload o;
o
= new xml.sz.luohuedu.net.aspxWebCS.Upload();
fs =
(System.IO.Stream)oFiles[0].InputStream;
fs.Read(b, 0,
oFiles[0].ContentLength);
///調(diào)用Web
Services的UploadFile方法進(jìn)行上載文件。
Response.Write(o.UploadFile(b,
FileName));
fs.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
else
{
Response.Write("請(qǐng)選擇文件");
}
}
}
}
最后,需要注意的是:在保存文件時(shí),您應(yīng)該確保指定文件的完整路徑(例如,"C:\MyFiles\Picture.jpg"),并確保為 ASP.NET 使用的帳戶提供要存儲(chǔ)文件的目錄的寫權(quán)限。上載大文件時(shí),可使用 元素的 maxRequestLength 屬性來增加文件大小的最大允許值,例如:
<configuration>
<system.web>
<httpRuntime
maxRequestLength="1048576" executionTimeout="3600" />
</system.web>
</configuration>
其中:maxRequestLength:指示 ASP.NET 支持的HTTP方式上載的最大字節(jié)數(shù)。該限制可用于防止因用戶將大量文件傳遞到該服務(wù)器而導(dǎo)致的拒絕服務(wù)攻擊。指定的大小以 KB 為單位。默認(rèn)值為 4096 KB (4 MB)。executionTimeout:指示在被 ASP.NET 自動(dòng)關(guān)閉前,允許執(zhí)行請(qǐng)求的最大秒數(shù)。在當(dāng)文件超出指定的大小時(shí),如果瀏覽器中會(huì)產(chǎn)生 DNS 錯(cuò)誤或者出現(xiàn)服務(wù)不可得到的情況,也請(qǐng)修改以上的配置,把配置數(shù)加大。
另外,上載大文件時(shí),還可能會(huì)收到以下錯(cuò)誤信息:
aspnet_wp.exe (PID: 1520) 被回收,因?yàn)閮?nèi)存消耗超過了 460 MB(可用 RAM 的百分之 60)。
如果遇到此錯(cuò)誤信息,請(qǐng)?jiān)黾討?yīng)用程序的 Web.config 文件的 元素中 memoryLimit 屬性的值。例如:
<configuration>
<system.web>
<processModel
memoryLimit="80"/>
</system.web>
</configuration>
聯(lián)系客服