ASP.NET导出Excel,如果文件名包含字符#或/,则在弹出打开/保存对话框时将其转换为下划线(_)!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Windows(或大多数其他操作系统)下,文件名不能包含字符
/
,因此您将无法阻止该文件的转换。大多数浏览器都不会使用#
,其中 IE 除外。Content-Disposition
文件名有一点限制。非 ASCII 字符在其中并不可靠,并且根据 RFC 的正确转义不起作用。如果您想将 Unicode 字符添加到默认下载文件名中(或某些其他标点符号,包括 IE 中的#
),您必须对其进行 URL 编码,并将其作为路径的尾部部分包含在内,省略内容处置文件名。例如:myscript.aspx:(
您仍然不能以这种方式在文件名中包含
/
,因为 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:myscript.aspx:
(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.)遗憾的是这是 IE 的限制。
根据 Microsoft 的说法,如果您在文件附件的文件名属性中使用以下字符,它们将被转换为下划线:
请参阅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:
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