如XML、PDF之类的文件,我们提供给客户下载时往往会在浏览器中打开,很不方便。
下面的demo可以解决此问题,尽管这段代码很多人都会,但还是发在这里,作为一个备份吧
<%@ Import Namespace="System.Xml" %>
<script language="C#" runat="Server">
void Page_Load(object sender, EventArgs e) {
string path = Server.MapPath("somefile.pdf");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
</script>
<script language="C#" runat="Server">
void Page_Load(object sender, EventArgs e) {
string path = Server.MapPath("somefile.pdf");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
</script>