ASP.Net MVC FileStreamResult,FileDownloadName 的有效字符
我有一个返回 FileStreamResult 的操作方法,下载工作正常,问题是虽然我设置了结果对象的 FileDownloadName 属性,但某些文件是以另一个名称下载的(特别是页面地址的最后一部分)我正在处理,例如在页面“http://localhost:5479/Items/Edit/277 ”它将下载一个名为“277”的文件)。
当文件名包含特殊字符(例如“San José.jpg”)时,就会发生这种情况,但当名称不包含此类字符(例如“San Jose.jpg”)时,它就可以正常工作。
所以,我的问题是,如何允许用户下载名称中包含特殊字符的文件?或者,如果不可能,是否有一种方法可以从字符串中删除所有特殊字符,或者我是否必须创建一个?
谢谢
I have an action method that returns a FileStreamResult, the download works fine, the problem is that although I set the FileDownloadName property of the result object, some of the files are downloaded with another name (specifically the last part of the address of the page I'm working on. e.g. in the page "http://localhost:5479/Items/Edit/277" it will download a file called "277").
This happens when the name of the file contains special chars (e.g. "San José.jpg"), but it works just fine when the name doesn't have such chars (e.g. "San Jose.jpg").
So, my question is, how do I allow the user to download a file with special chars in its name? or, if it isn't possible, is there a method that removes all the special chars from a string or do I have to create one?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上我只是找到了解决这个问题的方法。
基本上我要做的就是使用 HttpUtility.UrlEncode 方法来转换文件的名称,当下载文件时,它将获得与原始文件几乎相同的名称(区别在于空格替换为加号(+)符号)。
希望这对其他人有帮助。
Actually I just found a way to fix this.
Basically what I have to do is use the HttpUtility.UrlEncode method to convert the name of the file, when the file is downloaded it will get almost the same name as the original file (the difference being the spaces replaced with a plus (+) sign).
Hope this helps someone else.
我有同样的问题。更好的解决方案是
HttpUtility.UrlPathEncode(...)
因为它不会用“+”替换空格。I have the same Problem. A better solution is
HttpUtility.UrlPathEncode(...)
since it doesn't replace space by '+'.