How to force the file name extension of the loaded gzipped data URI?

I have a GZipped JavaScript file that I encode using a data URI:

For instance:

<a href="data:application/x-gzip;base64,FOO">My Javascript File</a> 

When the user right-clicks and saves this file (in Chrome), I would like the resulting file name to be download.js.gz . Thus, when the user double-clicks this file (on Mac OS X), it decomposes and renames correctly to download.js , and they can easily view the contents.

The problem is that when I use the content type text/javascript (or application/x-javascript ), the file is saved as download.js . And when I use the application/x-gzip content type, the file is saved as download.gz .

Can I save the file as .js.gz ?

Here is a working example of both links: http://jsfiddle.net/xYm8w/3/

The file should be the uri of the data, not a link to the actual file. I tried to change the text between the <a> tags, but it does not affect anything.

+4
source share
1 answer

In browsers other than Chrome, there seems to be no way to do this. Therefore, the best option that I could think of is to inform the user about saving the file in normal mode (so that it automatically inserts the appropriate extension based on the type of content), and then forcing them to go into finder and rename the file, .gz '.

In Chrome, however, you can use the download attribute of the <a> tag (which I found through this answer ). Example: http://jsfiddle.net/pYpqW/

+4
source

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


All Articles