一般处理程序 ——下载文件

 

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace _2_一般处理程序
{
    /// <summary>
    /// 下载文件 的摘要说明
    /// </summary>
    public class 下载文件 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //增加Content-Disposition是告诉浏览器,这个返回的内容是“附件形式”要给用户保存
            //filename是建议的文件名
            context.Response.AddHeader("Content-Disposition", "attachment;filename=" 
                + context.Server.UrlEncode("动态文件.txt"));
            DataTable dt = SQLHelper.ExecuteQuery("select top 100 id,code,parentId,name,level from Area");
            foreach (DataRow dr in dt.Rows)
            {
                context.Response.Write(dr["id"] + " | " + dr["code"] + " | " 
                    + dr["parentId"] + " | " + dr["name"] 
                    + " | " + dr["level"] + "\r\n");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  

 

posted @ 2015-05-21 22:34  linyongqin  阅读(430)  评论(0编辑  收藏  举报