<a href="1.jpg">显示图片</a>会在浏览器直接打开图片,C#实现不直接打开而是下载

页面代码:

<div>
        <a href="1.jpg">显示图片</a>
        <a href="Handler3.ashx">下载图片</a>
</div>

Handler3.ashx代码:

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

namespace TestProject
{
    /// <summary>
    /// Handler3 的摘要说明
    /// </summary>
    public class Handler3 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/JPEG";
            context.Response.AddHeader("Content-Disposition", "attachment;filename=haha.jpg");
            context.Response.WriteFile("1.jpg");
            //context.Response.Write("Hello World");
        }

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

在响应头中添加Content-Disposition:attachment;filename=haha.jpg使图片不直接在浏览器中显示,而是下载

posted @ 2013-08-12 21:48  ipangjie  阅读(1554)  评论(0)    收藏  举报