首页 > 其他 > 详细

将一个文件作为下载的形式打开【下载文件】,添加报文头context.Response.AddHeader("Content-disposition", "attachment; filename="

时间:2014-08-13 14:59:56      阅读:1075      评论:0      收藏:0      [点我收藏+]

<1>

HTMLPage1.htm页

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<a href="下载Handler1.ashx">下载</a>
</body>
</html>


下载Handler1.ashx.cs

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

namespace WebApplication3
{
    /// <summary>
    /// 下载Handler1 的摘要说明
    /// </summary>
    public class 下载Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/JPEG";
            string fileName=context.Server.UrlDecode("haha.jpg"); 

            //添加报文头,只有在报文里添加了"Content-disposition", "attachment; filename="打开文件的时候就会以下载文件的形式弹出,
            //如果不加这个报文头的话,就是将"/images/111.jpg"这个文件写入到浏览器,就是直接在浏览器中呈现出图片了
            //attachment; filename="+ fileName其实就是下载的时候将111.jpg这个文件取个“haha”的别名

            context.Response.AddHeader("Content-disposition", "attachment; filename=" + fileName); 
            
            //要下载的文件名
            context.Response.WriteFile("/images/111.jpg");    //----注意:WriteFile

            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
bubuko.com,布布扣




将一个文件作为下载的形式打开【下载文件】,添加报文头context.Response.AddHeader("Content-disposition", "attachment; filename=",布布扣,bubuko.com

将一个文件作为下载的形式打开【下载文件】,添加报文头context.Response.AddHeader("Content-disposition", "attachment; filename="

原文:http://blog.csdn.net/fanbin168/article/details/38535735

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!