Ruby on rails Rails发送文件名utf-8在Internet Explorer中损坏
我使用以下代码在rails中发送文件:Ruby on rails Rails发送文件名utf-8在Internet Explorer中损坏,ruby-on-rails,Ruby On Rails,我使用以下代码在rails中发送文件: send_file(file_to_send, :x_sendfile => true, :filename => file_name, :type => file_mime_type, :disposition => disposition, :stream => true, :buffer_size => 4096) 其中file_nam
send_file(file_to_send,
:x_sendfile => true,
:filename => file_name,
:type => file_mime_type,
:disposition => disposition,
:stream => true,
:buffer_size => 4096)
其中file_name
包含一个utf-8文件名,如lörem ipsüm.docx
。
在Firefox中,Chrome可以正常工作。在InternetExplorer甚至Edge中,特殊的德语字符(可能还有所有非默认字符)被破坏
下载的文件名是Lörem ipsuÌm.docx
在做了许多其他事情之后,我尝试了:filename=>URI.encode(file\u name)
,这在IE和Edge中找到了正确的文件名,但在Chrome和FF中却没有。在那里我确实得到了编码的文件名Lo%CC%88rem%20ipsu%CC%88m.docx
因此,任何人都知道我如何解决这个问题,使其在所有浏览器上都能工作?由于有人可能会遇到相同的问题,以下是解决方案:
send_file(file_to_send,
:filename => ERB::Util.url_encode(file_name),
:x_sendfile => true,
:type => file_mime_type,
:disposition => "#{disposition}; filename*= UTF-8''#{ERB::Util.url_encode(file_name)}", # MSIE & MSEdge requirement to support non Latin filenames
:stream => true,
:buffer_size => 4096)