execCommand SaveAs在Firefox中工作吗? [英] Does execCommand SaveAs work in Firefox?

查看:196
本文介绍了execCommand SaveAs在Firefox中工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在ff / chrome中不行?

  javascript:document.execCommand('SaveAs','true' ,的 'http://www.google.com'); 

(用作书签)

解决方案

execCommand在浏览器中并没有完全标准化。事实上,execCommand('SaveAs',...)似乎只支持IE。强制保存的推荐方法是使用content-disposition:attachment标头,如 > http://www.jtricks.com/bits/content_disposition.html



因为这是HTTP头的一部分,所以可以使用它任何文件类型。如果您使用的是Apache,您可以使用.htaccess文件添加标题,如这里。例如:

 < FilesMatch\.pdf $> 
< IfModule mod_headers.c>
Header set Content-Dispositionattachment
#用于旧浏览器
头文件集内容类型application / octet-stream

< / FilesMatch>


Why does this not work in ff/chrome?

javascript: document.execCommand('SaveAs','true','http://www.google.com');

(used as a bookmarklet)

解决方案

execCommand is not completely standardized across browsers. Indeed, execCommand('SaveAs', ...) only seems to be supported on IE. The recommended way to force a save-as would be to use a content-disposition: attachment header, as described in http://www.jtricks.com/bits/content_disposition.html

Since this is part of the HTTP header, you can use it on any file type. If you're using apache, you can add headers using the .htaccess file, as described here. For example:

<FilesMatch "\.pdf$">
<IfModule mod_headers.c>
Header set Content-Disposition "attachment"
# for older browsers
Header set Content-Type "application/octet-stream"
</IfModule>
</FilesMatch>

这篇关于execCommand SaveAs在Firefox中工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆