Chrome cannot recognize url encoded file name at boot time

I tried to download a file with the file name GB2312% D5% D5% C6% AC.JPG from my site, everything works fine in IE9 / Firefox, but not in Google Chrome.

In Google Chrome, this file name is replaced with my download page name (maybe Chrome could not decode the file name).

To find out if Chrome translates to decode the file name, I tried using a different line GB2312% 2C% 2D% 2E.txt as the file name, firefox / IE9 is still working properly, but Google Chrome will try to decode this file name (replace % 2D to "-").

How to prevent Google Chrome from decoding a file name? Better if I can solve this problem on my server side (PHP)

The following lines are the response headers generated by my server.

**Response Headers:** Cache-Control:must-revalidate, post-check=0, pre-check=0, private Connection:keep-alive Content-Description:File Transfer Content-Disposition:attachment; filename="GB2312%D5%D5%C6%AC.JPG" Content-Length:121969 Content-Transfer-Encoding:binary Content-Type:application/force-download; charset=UTF-8 Date:Wed, 18 Apr 2012 03:32:30 GMT Expires:0 Pragma:public Server:nginx/1.1.5 X-Powered-By:PHP/5.3.8 
+6
source share
1 answer

I just had this problem. This question was helpful.

The reason, as you pointed out, is that Chrome is trying to decode it, and not outside of ASCII. I would call it a mistake personally.

Basically, the answer to make it work in Chrome is to use:

 Content-Disposition:attachment; filename*=UTF-8''GB2312%D5%D5%C6%AC.JPG 

However, this will break it in IE8 and Safari. Ugh! To make it work in this, try to do it like this example .

 Content-Disposition:attachment; filename="GB2312%D5%D5%C6%AC.JPG"; filename*=UTF-8''GB2312%D5%D5%C6%AC.JPG 

For me, I had to encode the file name before putting it in the file name * = UTF-8. So my values ​​for the first file name and the second value do not match. I used PHP and my code is as follows:

 $encodedfilename = urlencode($downloadfilename); header("Content-Disposition: attachment; filename=\"$downloadfilename\"; filename*=UTF-8''$encodedfilename"); 

This way, I don't actually stop it from encoding, as you requested, but I encode it, and then pass the parameter that Chrome decodes back to what it should.

+3
source

Source: https://habr.com/ru/post/913484/


All Articles