asp.net - 从浏览器选项卡下载的 Word 文件文件名不正确
问题描述
我试图.docx
在浏览器上打开一个文件,但它被下载了。这很好,但下载的.docx
文件名不正确。
我正在使用 Chrome 浏览器和 ASP.NET C# 代码。
假设该文件位于网络路径//test/test.docx
和 aspx 文件名DownloadTest.aspx
中,其中包含下载 word doc 代码。当它被下载时,文件名是DownloadTest.docx
而不是test.docx
.
下面是代码。
if (!this.IsPostBack)
{
string filePath = Request.QueryString["FN"];
Page.Title = filePath;
this.Response.ClearContent();
this.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
this.Response.AppendHeader("Content-Disposition;", "attachment;filename=" + Request.QueryString["FN"]);
this.Response.WriteFile(filePath);
this.Response.End();
}
解决方案
将查询字符串参数的值更改为FN
网络文件名 (test.docx)。标Content-Disposition
头确定下载时浏览器中的文件名。
换句话说,该标题需要
Content-Disposition: attachment;filename=test.docx
所以你的代码应该是
this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fn);
其中 fn 等于 test.docx,它可能不是请求参数。在对它们进行任何操作之前,始终检查请求参数值并验证它们是一个好主意。
另请注意,当您添加标题时,不要在其名称后放置分号。
推荐阅读
- reactjs - 如何在 Antdesign + React.js App 中添加自定义翻译
- driver - 如何在 october CMS 中使用 SFTP 驱动程序
- testing - 自动化验收测试中的代码重用,无需过多抽象
- scala - 在 Cats 中解析三等号的确切导入是什么?
- python-3.x - Python检查给定的多个子字符串是否在列表中
- java - 为什么要在方法中引入新数组?为什么不重用参数?
- java - 检索特定于一个记录器类的前 n 行 log4j
- ios - 我不明白为什么我的对象没有收到通知
- pandas - 从熊猫系列中删除列表中未找到的单词
- android - “!!”是什么意思 运算符在 Kotlin 中是什么意思?