代码之家  ›  专栏  ›  技术社区  ›  Abdelrahman ELGAMAL

文件下载问题:文件名的空格被截断!

  •  16
  • Abdelrahman ELGAMAL  · 技术社区  · 14 年前

    当我使用以下代码从服务器下载文件时:

    Response.AddHeader("Content-Disposition", "attachment; filename=" + 
    Server.UrlPathEncode(Path.GetFileName(_Filename)));
    

    问题是文件名中有空格时,使用此代码,服务器在查找第一个空格时会自动拆分!

    我希望知道为什么解决这个问题的方法是什么?

    4 回复  |  直到 6 年前
        1
  •  41
  •   ChrisF    14 年前

    您需要用双引号将文件名括起来。

    string filename = Server.UrlPathEncode(Path.GetFileName(_Filename)));
    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    

    否则,代码假定文件名以第一个空格结尾。

    你可能不需要 Server.UrlPathEncode .

        2
  •  5
  •   Nazmul    6 年前

    我找到了解决方案:)

    我们必须用双引号将文件名括起来,例如:

    response.addheader(“内容处置”,“附件;文件名=\”“+path.getFileName(_文件名)+”“\”“);

    但到现在为止,我还不知道这件事吗?

        3
  •  1
  •   amarsuperstar    14 年前

    尝试引用文件名,而不是像这样对其进行编码

    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(_Filename) + "\"");
    
        4
  •  0
  •   jflaga    11 年前

    火狐就是这样……

    我在这里找到了Alfonso Martinez的答案: https://bugzilla.mozilla.org/show_bug.cgi?id=221028#c1

    [Alfonso Martinez]和Christian在Mozillazine讨论这个问题 Biesinger和Boris Zbarsky,他们说这是正确的行为 根据RFC。

    解决方案就是把引用的文件名放进去,然后一切都能正常工作。 很好,因为这是预期的语法。