标签:content-disposition addheader
<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;
}
}
}
}
将一个文件作为下载的形式打开【下载文件】,添加报文头context.Response.AddHeader("Content-disposition", "attachment; filename="
标签:content-disposition addheader
原文地址:http://blog.csdn.net/fanbin168/article/details/38535735