I use the following code to send a file in rails:
send_file(file_to_send,
:x_sendfile => true,
:filename => file_name,
:type => file_mime_type,
:disposition => disposition,
:stream => true,
:buffer_size => 4096)
where file_name
contains a utf-8 filename like lörem ipsüm.docx
.
In Firefox, Chrome it works fine. In Internet Explorer and even Edge the special German chars (and probably all non default chars) are broken.
The filename on download is Lörem ipsüm.docx
.
After many other things I tried :filename => URI.encode(file_name)
which leads to a correct filename in IE and Edge, BUT not in Chrome and FF. There I do get the encoded filename Lo%CC%88rem%20ipsu%CC%88m.docx
.
So anyone has a clue how I can fix this that it works on all browsers?
As someone might run into same problem, here is the solution:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With