如何在Content-Disposition标头中设置包含空格的文件名

时间:2022-04-16 16:10:35

I have this piece of code:

我有这段代码:

resp.addHeader("Content-Disposition", "inline; filename=" + fileName);

When the file name is "a_b_c.doc" or "abc.doc" the name of the downloaded file is displayed correctly. However, when the file name is "a b c .doc" the name of the downloaded file is only "a".

当文件名是“a_b_c.doc”或“abc.doc”时,正确显示下载文件的名称。但是,当文件名是“a b c .doc”时,下载文件的名称只是“a”。

How can we solve this?

我们怎么解决这个问题?

3 个解决方案

#1


53  

Use quotes:

resp.addHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");

#2


7  

According to the HTTP standard you surround the string with double-quotes, and escape any quotes or backslashes within by preceding them with a single backslash.

根据HTTP标准,您使用双引号括起字符串,并通过在其前面使用单个反斜杠转义任何引号或反斜杠。

Content-Disposition: attachment; filename="Very \"interesting\" file \\ files.txt"

This will prompt to save as Very "interesting" file \ files.txt. Note that the presence of a backslash does not suggest a folder, it suggests the backslash is part of the filename (which is perfectly valid on Linux and some other platforms, but not on Windows.)

这将提示保存为非常“有趣”的文件\ files.txt。请注意,反斜杠的存在并不表示文件夹,它表明反斜杠是文件名的一部分(在Linux和其他一些平台上完全有效,但在Windows上则不行。)

#3


1  

if you quote your filename with chr(34) it will work:

如果你用chr(34)引用文件名,它将起作用:

resp.addHeader("Content-Disposition", "inline; filename=" + chr(34) + fileName + chr(34));

#1


53  

Use quotes:

resp.addHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");

#2


7  

According to the HTTP standard you surround the string with double-quotes, and escape any quotes or backslashes within by preceding them with a single backslash.

根据HTTP标准,您使用双引号括起字符串,并通过在其前面使用单个反斜杠转义任何引号或反斜杠。

Content-Disposition: attachment; filename="Very \"interesting\" file \\ files.txt"

This will prompt to save as Very "interesting" file \ files.txt. Note that the presence of a backslash does not suggest a folder, it suggests the backslash is part of the filename (which is perfectly valid on Linux and some other platforms, but not on Windows.)

这将提示保存为非常“有趣”的文件\ files.txt。请注意,反斜杠的存在并不表示文件夹,它表明反斜杠是文件名的一部分(在Linux和其他一些平台上完全有效,但在Windows上则不行。)

#3


1  

if you quote your filename with chr(34) it will work:

如果你用chr(34)引用文件名,它将起作用:

resp.addHeader("Content-Disposition", "inline; filename=" + chr(34) + fileName + chr(34));