如何强制下载的 gzip 数据 URI 的文件扩展名?
我有一个 GZipped JavaScript 文件,我正在使用 data:URI 对其进行编码。
例如:
<a href="data:application/x-gzip;base64,FOO">My Javascript File</a>
当用户右键单击并保存此文件(在 Chrome 中)时,我希望生成的文件名称为 download.js.gz
。这样,当用户双击该文件(在 Mac OS X 中)时,它会被解压缩并正确重命名为 download.js
,并且他们可以轻松查看内容。
问题是,当我使用内容类型 text/javascript
(或 application/x-javascript
)时,文件会保存为 download.js
>。当我使用内容类型 application/x-gzip
时,文件将保存为 download.gz
。
有什么方法可以将文件保存为 .js.gz
吗?
这是两个链接的工作示例: http://jsfiddle.net/xYm8w/3/
文件必须是数据 uri,而不是实际文件的链接。我尝试更改 标记之间的文本,但它根本不会影响任何内容。
I have a GZipped JavaScript file which I am encoding with a data:URI.
For example:
<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's name to be download.js.gz
. This way when the user double clicks this file (in Mac OS X), it gets decompressed and renamed correctly to download.js
, and they can view the contents easily.
The problem is that when I use the content type text/javascript
(or application/x-javascript
), the file gets saved as download.js
. And when I use the content type application/x-gzip
, the file gets saved as download.gz
.
Is there any way to get the file to be saved as .js.gz
?
Here's a working example of both links: http://jsfiddle.net/xYm8w/3/
The file must be a data uri and not a link to an actual file. I tried changing the text between the <a>
tags, but it doesn't affect anything at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了 Chrome 之外,似乎没有其他浏览器可以做到这一点。因此,我能想到的最好的选择是通知用户正常保存文件(以便它根据内容类型自动插入正确的扩展名),然后让他们进入查找器并重命名文件以附加“ .gz' 扩展名。
不过,在 Chrome 上,您可以使用
标记的
download
属性(我通过 这个答案)。示例: http://jsfiddle.net/pYpqW/There doesn't seem to be a way to do this in browsers besides Chrome. Therefore, the best option I could think of is to inform the user to save the file normally (so that it automatically inserts the proper extension based on the content type), and then having them go into finder and renaming the file to append the '.gz' extension.
On Chrome, though, you can use the
download
attribute of the<a>
tag (which I found out by this answer). Example: http://jsfiddle.net/pYpqW/