简体   繁体   English

execCommand SaveAs在Firefox中有效吗?

[英]Does execCommand SaveAs work in Firefox?

Why does this not work in ff/chrome? 为什么这不适用于ff / chrome?

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

(used as a bookmarklet) (用作书签)

execCommand is not completely standardized across browsers. execCommand没有完全标准化的浏览器。 Indeed, execCommand('SaveAs', ...) only seems to be supported on IE. 实际上,execCommand('SaveAs',...)似乎只在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 强制保存的推荐方法 - 如使用内容处置:附件标头,如http://www.jtricks.com/bits/content_disposition.html中所述

Since this is part of the HTTP header, you can use it on any file type. 由于这是HTTP标头的一部分,因此您可以在任何文件类型上使用它。 If you're using apache, you can add headers using the .htaccess file, as described here . 如果你使用Apache,您可以使用.htaccess文件添加标题,描述在这里 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>

It is possible to do this in Firefox via data URIs (see also Download data url file ) and optionally via the download attribute. 可以通过数据URI (参见下载数据URL文件 )和可选的下载属性在Firefox中执行此操作。

See http://html5-demos.appspot.com/static/a.download.html for an HTML5 shim demo. 有关HTML5 shim演示,请参见http://html5-demos.appspot.com/static/a.download.html

How to force save as dialog box in firefox besides changing headers? 除了更改标题之外,如何在firefox中强制另存为对话框? also covers this topic. 也涵盖了这个主题。

You can also test it by the following Firefox-tested demo. 您还可以通过以下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>

The following also works for URLs as well as JavaScript-initiated loads without the download attribute (though this approach does not allow a file name, it does allow a preview in a new tab): 以下内容也适用于URL以及没有下载属性的JavaScript启动的加载(尽管此方法不允许使用文件名,但它允许在新选项卡中进行预览):

<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 doesn't support execCommand. Firefox不支持execCommand。 In fact it seems to be IE-only. 事实上,它似乎只是IE浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM