C# 一般处理程序下载文件

时间:2023-02-20 19:31:23
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace test
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string loadname = context.Request["loadname"];

            string curBrowser = context.Request.Browser.Type.ToLower();

            string path = "/resource/" + loadname;
             
            //对ie和chrome需要设置url编码
            if (curBrowser.Contains("internetexplorer") || curBrowser.Contains("chrome"))
            {
                loadname = HttpUtility.UrlEncode(loadname); 
            }

           
           
            //设置消息头
            context.Response.AddHeader("Content-Disposition", "attachmen;filename=" + loadname);
            //下载文件
            context.Response.WriteFile(path);


        }

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