利用“一般处理程序”。如果你实用的是vs2005你在添加新项中可以见到“一般处理程序”,它的后缀名为.ashx。它是什么?其实它和.aspx很类似。先问问.aspx是如何工作的?也许你知道,.aspx能处理来自外部传入的请求,然后它还能处理这个请求并生成一个html作为结果返回。这是典型的处理外部请求的方式。.aspx就是专门为处理“典型”的请求而出现的。那么如果我们现在需要一种又能处理外部请求又需要我们自定义的处理这个请求那又要怎么做呢?(也就是不实用“典型”的方式来处理)。.ashx就能帮你做到这一点。
如果返回的是非html内容,请用一般处理程序实现
一 文件下载
1 html代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>文件打开与保存</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="Default.aspx">打开文件</a>
<a href="Download.ashx">保存文件</a>
</div>
</form>
</body>
</html>
2 添加一个一般处理程序
<%@ WebHandler Language="C#" Class="Download" %>
using System;
using System.Web;
using System.IO;
public class Download : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//添加一个文件的数据类型
context.Response.ContentType = "application/octet-stream";
//将文件进行编码
string encodeFileName = HttpUtility.UrlEncode("文件.htm");//需要把文件名UrlEncode,因为有中文存在
//添加一个报文头
context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", encodeFileName));
//获得文件的文件名称
string filepath = context.Server.MapPath("Default.aspx");
//利用文件流保存
context.Response.Write(File.ReadAllText(filepath));
//添加一个文件的数据类型
//context.Response.ContentType = "application/octet-stream";
//将文件进行编码
//string encodeFileName = HttpUtility.UrlEncode("文件.htm");
//获得文件的文件名称
//string filepath = context.Server.MapPath("Default.aspx");
//添加一个报文头
//context.Response.AddHeader("Content-Disposition","attachment;filename="+encodeFileName);
//context.Response.AddHeader("Content-Length",encodeFileName.Length.ToString());
//输入文件到文件流中
//context.Response.WriteFile(filepath);
//context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
3 运行效果如下:
单击“保存文件”时,出现以下对话框

不再是普通是输出文件,而是弹出对话框,原因在于http报文头中添加了一个报文头,是下载,查看报文头,可以看到如下信息:

如果是单击“打开文件”,则看到报文信息如下:

以上比较说明http报文头对输出到客户端时的不同影响
二 EXCEL文件下载(注意添加对nopi dll文件的引用)

1 添加一个一般处理程序
<%@ WebHandler Language="C#" Class="DownExcel" %>
using System;
using System.Web;
public class DownExcel : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//指定输出类型
context.Response.ContentType = "application/x-excel";
//指定文件编码
string filename = HttpUtility.UrlEncode("客户资料表.xls");
//添加http报文头输出类型
context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", filename));
//创建excel文件表的标题
NPOI.HSSF.UserModel.HSSFWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.HSSF.UserModel.HSSFSheet sheet = workbook.CreateSheet();
NPOI.HSSF.UserModel.HSSFRow row = sheet.CreateRow(0);
NPOI.HSSF.UserModel.HSSFCell cell0 = row.CreateCell(0, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
cell0.SetCellValue("客户名称");
NPOI.HSSF.UserModel.HSSFCell cell1 = row.CreateCell(1, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
cell1.SetCellValue("车辆类型");
NPOI.HSSF.UserModel.HSSFCell cell2 = row.CreateCell(2, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
cell2.SetCellValue("购买价格");
//输出相关客户信息
for (int x = 1; x < 8; x++)
{
NPOI.HSSF.UserModel.HSSFRow rowx = sheet.CreateRow(x);
NPOI.HSSF.UserModel.HSSFCell c0 = rowx.CreateCell(0, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
c0.SetCellValue("cust"+x.ToString());
NPOI.HSSF.UserModel.HSSFCell c1 = rowx.CreateCell(1, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
c1.SetCellValue(x.ToString() + x.ToString());
NPOI.HSSF.UserModel.HSSFCell c2 = rowx.CreateCell(2, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
c2.SetCellValue(x * 200);
}
//将文本流输出到客户端
workbook.Write(context.Response.OutputStream);
}
public bool IsReusable {
get {
return false;
}
}
}
<head runat="server">
<title>EXCEL文件下载示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<hr />
<a href="DownExcel.ashx">下载excel文件</a>
<hr />
</div>
</form>
</body>
</html>
3 运行时效果:



using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
{
protected void Page_Load(object sender, EventArgs e)
{
protected void Button1_Click(object sender, EventArgs e)
{
//不推荐这个方法来实现下载
Response.Clear();
Response.ContentType = "application/x-excel";
//指定文件编码
string filename = HttpUtility.UrlEncode("客户资料表.xls");
//添加http报文头输出类型
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", filename));
//创建excel文件表的标题
NPOI.HSSF.UserModel.HSSFWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.HSSF.UserModel.HSSFSheet sheet = workbook.CreateSheet();
NPOI.HSSF.UserModel.HSSFRow row = sheet.CreateRow(0);
NPOI.HSSF.UserModel.HSSFCell cell0 = row.CreateCell(0, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
cell0.SetCellValue("客户名称");
NPOI.HSSF.UserModel.HSSFCell cell1 = row.CreateCell(1, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
cell1.SetCellValue("车辆类型");
NPOI.HSSF.UserModel.HSSFCell cell2 = row.CreateCell(2, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
cell2.SetCellValue("购买价格");
//输出相关客户信息
for (int x = 1; x < 8; x++)
{
NPOI.HSSF.UserModel.HSSFRow rowx = sheet.CreateRow(x);
NPOI.HSSF.UserModel.HSSFCell c0 = rowx.CreateCell(0, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
c0.SetCellValue("cust" + x.ToString());
NPOI.HSSF.UserModel.HSSFCell c1 = rowx.CreateCell(1, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
c1.SetCellValue(x.ToString() + x.ToString());
NPOI.HSSF.UserModel.HSSFCell c2 = rowx.CreateCell(2, NPOI.HSSF.UserModel.HSSFCell.CELL_TYPE_STRING);
c2.SetCellValue(x * 200);
}
//将文本流输出到客户端
workbook.Write(Response.OutputStream);
Response.End();
}
}
3 运行结果如下:

