存档

文章标签 ‘HTML下载’

ASP.NET将HTML作为附件下载

2010年11月20日 没有评论

有时候我们希望将aspx生成的HTML作为一个附件让用户下载,可是对于HTML文件,浏览器会直接显示,而不是提示用户保存。解决这个问题的方法很简单,只要在Page_Load事件中加入几行代码即可:

protected void Page_Load(object sender, EventArgs e)
{
string reportFileName = “报表.html”;
Response.ContentEncoding = System.Text.Encoding.GetEncoding(“UTF-8”); //解决中文乱码
Response.AddHeader(“Content-Disposition”, “attachment; filename=” + Server.UrlEncode(reportFileName));
Response.ContentType = “appliction/octet-stream”;
}