ASP.NET导出Excel,如果文件名包含字符#或/,则在弹出打开/保存对话框时将其转换为下划线(_)!

发布于 2024-10-06 03:14:16 字数 278 浏览 5 评论 0原文

string filename = "Category#FileName";

response.AddHeader("content-disposition", "attachment; filename=" + filename);
response.ContentType = "text/csv";
response.AddHeader("Pragma", "public");

最终文件名=> Category_FileName

我非常感谢您的帮助,请帮忙!

string filename = "Category#FileName";

response.AddHeader("content-disposition", "attachment; filename=" + filename);
response.ContentType = "text/csv";
response.AddHeader("Pragma", "public");

Final file name => Category_FileName

I would appreciate your help here, pls help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一笑 2024-10-13 03:14:16

在 Windows(或大多数其他操作系统)下,文件名不能包含字符 /,因此您将无法阻止该文件的转换。大多数浏览器都不会使用 #,其中 IE 除外。

Content-Disposition 文件名有一点限制。非 ASCII 字符在其中并不可靠,并且根据 RFC 的正确转义不起作用。如果您想将 Unicode 字符添加到默认下载文件名中(或某些其他标点符号,包括 IE 中的 #),您必须对其进行 URL 编码,并将其作为路径的尾部部分包含在内,省略内容处置文件名。例如:

http://www.example.com/myscript.aspx/foo%23bar.xls

myscript.aspx:(

response.AddHeader("Content-Disposition", "attachment");

您仍然不能以这种方式在文件名中包含 /,因为 Web 服务器往往会阻止所有带有 %2F 的 URL。无论如何,如上所述,您不会无论如何都可以用文件名中的 / 来保存它。)

A filename can't contain the character / under Windows (or most other OSes), so you won't be able to stop that one getting converted. # is left alone by most browsers, the odd one out being IE.

Content-Disposition filenames are a bit limited. Non-ASCII characters aren't reliable in them and the proper escaping according to the RFC doesn't work. If you want to get Unicode characters into a default download filename​—or certain other punctuation characters including # in IE—you have to URL-encode it and include that as a trailing part of the path, omitting the Content-Disposition filename. eg:

http://www.example.com/myscript.aspx/foo%23bar.xls

myscript.aspx:

response.AddHeader("Content-Disposition", "attachment");

(You still can't include / in a filename this way as web servers tend to block all URLs with %2F in. In any case, as above, you wouldn't be able to save it with a / in the filename anyway.)

妥活 2024-10-13 03:14:16

遗憾的是这是 IE 的限制。

根据 Microsoft 的说法,如果您在文件附件的文件名属性中使用以下字符,它们将被转换为下划线:

<   Left angle bracket
>   Right angle bracket
\   Backslash
"   Quotation mark
/   Slash mark
:   Colon
|   Vertical bar
?   Question mark
*   Asterisk
    Space

请参阅https://support.microsoft.com/en-us/help/949197/certain-characters-in-a-file-name-may-be-converted-to-underscores-when-a -user-downloads-a-file-by-using-windows-internet-explorer-7

Sadly this is a limitation of IE.

According to Microsoft, the following characters will be converted to underscores if you use them in the file attachment's filename attribute:

<   Left angle bracket
>   Right angle bracket
\   Backslash
"   Quotation mark
/   Slash mark
:   Colon
|   Vertical bar
?   Question mark
*   Asterisk
    Space

See https://support.microsoft.com/en-us/help/949197/certain-characters-in-a-file-name-may-be-converted-to-underscores-when-a-user-downloads-a-file-by-using-windows-internet-explorer-7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文