Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails send_file filename utf-8 broken in Internet Explorer

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?

like image 352
YvesR Avatar asked Sep 15 '25 17:09

YvesR


1 Answers

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)
like image 103
YvesR Avatar answered Sep 17 '25 08:09

YvesR