在Firefox浏览器中,execCommand SaveAs命令是否有效?

7
为什么在火狐/谷歌浏览器中无法正常运行?
javascript: document.execCommand('SaveAs','true','http://www.google.com');

(用作书签)

感谢回复。更具体地说,我正在尝试在网络上的PDF文件上强制执行“另存为”。有没有办法在FF中实现这一点? - raj
如何在 Firefox 中强制显示“另存为”对话框,而不是更改标头? - hakre
4个回答

11

在Firefox中,可以通过数据URI(另请参见下载数据URI文件)来实现此功能,并可选使用下载属性。

请参考http://html5-demos.appspot.com/static/a.download.html 了解HTML5 shim演示。

如何在Firefox中强制保存对话框而无需更改标头? 也涵盖了此主题。

您还可以通过以下针对Firefox测试的演示进行测试。

<!DOCTYPE html>
<body>
<script>
var a = document.createElement('a');
//alert(a.download === ''); // If true, this seems to indicate support
a.setAttribute('download', 'testme.png');
a.href = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAwElEQVQ4jWNgGPRgv7Y2z0lj45STpqbHT5iaxhCt8biBgcJJU9PZJ01MPp80MfkPxZOJN8DEpAFJ4/+TJib/T5mY7CdK8wkTkwJ0zVA8naDmk0ZGPjg0/z9hbGyDV/MZY2ORkyYm77FpPmVispwSp6/e7+DAQtj5pqabsdi8myjNUANmY7H99jEjIxWiDDhuauqCxYDD+7W1eYgy4IyxMetJE5PpyH4/ZWqqTZRmGIAm3fsk2YwOjhkZqZCtmVQAAIOlmIi0XoodAAAAAElFTkSuQmCC';
a.innerHTML = 'testing';
a.style.display = 'none';
document.body.appendChild(a);
a.click();
</script>

以下内容同样适用于URL,以及没有下载属性的JavaScript-initiated加载(虽然这种方法不允许设置文件名,但它允许在新标签页中预览):

<script>
var myText = 'Hello world!', 
    myHTML = '<b>'+myText+'</b>';

function openFile (textToEncode, contentType, newWindow) {
    // For window.btoa (base64) polyfills, see 
    // https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills
    var encodedText = window.btoa(textToEncode);
    var dataURL = 'data:' + contentType + ';base64,' + encodedText;
    if (newWindow) { // Not useful for application/octet-stream type
        window.open(dataURL); // To open in a new tab/window
    }
    else {
        window.location = dataURL; // To change the current page
    }
}
</script>

<h1>Hello world files:</h1>

<p>Octet stream type to prompts download dialog in Firefox, but with no 
   default file type or path:</p> 

<a href="data:application/octet-stream;base64,SGVsbG8sIFdvcmxkIQ%3D%3D">
    (text example)</a>
<a href="data:application/octet-stream;base64,PGI+SGVsbG8gd29ybGQhPC9iPg==">
    (HTML example)</a>
<button onclick="openFile(myHTML, 'application/octet-stream');">
    (HTML example, from JavaScript)</button>

<p>Quickly viewable (and manually savable) in browser but no dialog presented:</p>
<a href="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D">(plain text, same window)</a>
<a href="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D" target="new-tab">
    (plain text--in new tab)</a>
<a href="data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E">(HTML, same window)</a>
<button onclick="openFile(myText, 'text/plain');">
    (text example, from JavaScript)</button>
<button onclick="openFile(myText, 'text/plain', true);">
     (text example, from JavaScript; open in new window)</button>
<button onclick="openFile(myHTML, 'text/html', true);">
   (HTML example, from JavaScript; open in new window)</button>

在提出不同问题的循环中,我来自于如何在Firefox中强制使用“另存为”对话框而不是更改标头? - Juto

10

execCommand在不同的浏览器中并没有完全标准化。实际上,execCommand('SaveAs', ...) 只似乎支持IE。强制保存文件的推荐方法是使用 content-disposition: attachment 头部,在 这里有描述。

由于这是HTTP头的一部分,您可以在任何文件类型上使用它。如果您正在使用apache,可以像 这里 描述的那样使用 .htaccess 文件添加头文件。例如:

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

我正在尝试在PDF文件上强制进行另存为操作。我认为我无法更改标题。 - raj
我认为内容分发是HTTP头的一部分,而不是文档的一部分,因此您应该能够将其用于PDF文件。 - Andrej
确实可以,这里就是一个例子 :) - bdonlan
哇,这是个很棒的想法。谢谢! 你知道有没有办法强制在不在我的服务器上的PDF文件上进行另存为吗? - raj
但更严肃地说,我不知道。尝试开启另一个关于那个特定主题的问题,也许会有其他人回答。 - bdonlan
好的,谢谢bdonlan。我从来没有想到过htaccess文件。 - raj

3

正如微软所说的那样,"没有适用于此方法的公共标准。"


0

Firefox不支持execCommand。实际上,它似乎只能在IE中使用。


有类似的 Firefox 功能吗? - raj
据我所知没有,您可以像bdonlan建议的那样使用content-disposition头。 - lc.
它(或至少是execCommand,而不是execCommand(saveAs))在MDN上有文档记录,支持Mozilla 1.3+。 - Rup

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接