码迷,mamicode.com
首页 > 其他好文 > 详细

下载图片

时间:2014-10-24 01:40:04      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   sp   文件   

public class DownLoad : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string fileName = context.Request.QueryString["filename"];
            //火狐浏览器支持中文传输,编码后反倒不认了,所以要判断排除火狐浏览器再编码
            string UserAgent = context.Request.ServerVariables["http_user_agent"].ToLower();
            if (UserAgent.IndexOf("firefox") == -1)
            {//非火狐浏览器
                fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
            }
            context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            context.Response.WriteFile(context.Server.MapPath("Down/") + context.Request.QueryString["filename"]); //这里一定要未编码的文件名字。否则找不到文件
        }

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

 

下载图片

标签:style   blog   http   color   io   os   ar   sp   文件   

原文地址:http://www.cnblogs.com/han1982/p/4047250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!