【问题标题】:Word file downloaded from browser tab with incorrect file name从浏览器选项卡下载的 Word 文件,文件名不正确
【发布时间】:2018-07-10 19:34:29
【问题描述】:

我试图在浏览器上打开一个.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();
}

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    将查询字符串参数FN 的值更改为网络文件名(test.docx)。 Content-Disposition 标头决定了下载时浏览器中的文件名。

    换句话说,该标题需要是

    Content-Disposition: attachment;filename=test.docx
    

    所以你的代码应该是

    this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fn);
    

    其中 fn 等于 test.docx,它可能不是请求参数。在对它们进行任何操作之前,始终检查请求参数值并对其进行验证是个好主意。

    另外请注意,当您添加标题时,不要在其名称后放置分号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-27
      • 2018-01-27
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      相关资源
      最近更新 更多