一般处理程序.ashx的应用

 

利用“一般处理程序”。如果你实用的是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    运行效果如下:

单击“保存文件”时,出现以下对话框

2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客

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

 

2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客

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

2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客

  以上比较说明http报文头对输出到客户端时的不同影响

 

二   EXCEL文件下载(注意添加对nopi dll文件的引用)

2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客

 

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;
        }
    }
}

 

2   html代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!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>EXCEL文件下载示例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <hr />
    <a   href="DownExcel.ashx">下载excel文件</a>
    <hr />
    </div>
    </form>
</body>
</html>
3   运行时效果:
2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客
 
2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客
 OK, 通过以上分析得知,服务器向客户机输出时excel文件并没有在服务器上,而是由一般处理程序动态产生的,利用文件流输出的
 
总结: 如果返非html的话,都写在一般处理程序中,而不是写在aspx文件中
三    利用aspx页面实现excel文件下载
1   UI设计如下;
2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客
 2   C#代码如下:
using System;
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;
public partial class Default6 : System.Web.UI.Page
{
    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   运行结果如下:
2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客
 
2010年12月6日    一般处理程序的应用 - ywjwest - ywjwest的博客
 结果与一般处理程序实现的结果是一样的,但是不推荐这种做法
总结: 如果返回的是非html内容,一定要用一般处理程序来实现
 
game over
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值