<%@ WebHandler Language="C#" Class="DownloadHandler" %>

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Linq;
//using BizDataAccess;
using System.IO;
using System.Net;
using System.Web.Services;
using System.Text;


public class DownloadHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string filePath = "../webpage/part";
        filePath = HttpContext.Current.Server.MapPath(filePath);
        string id = HttpContext.Current.Request["id"];
        filePath = filePath + "/" + id;
        string name = HttpContext.Current.Request["name"];
        try
        {
            System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            //HttpContext.Current.Response.ContentType = getContentType(extension);  
            //通知浏览器下载文件而不是打开  
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
            return;
        }
        catch (Exception ex)
        {
            return;
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
