execCommand SaveAs 在 Firefox 中工作吗?

发布于 2024-07-19 00:00:28 字数 147 浏览 11 评论 0原文

为什么这在 ff/chrome 中不起作用?

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

(用作书签)

Why does this not work in ff/chrome?

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

(used as a bookmarklet)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜宝宝 2024-07-26 00:00:28

可以通过 数据 URI 在 Firefox 中执行此操作(另请参阅下载数据 url 文件 ),也可以选择通过 download 属性。

请参阅 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 发起的加载(尽管此方法不允许使用文件名,但它允许在新选项卡中预览):

<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>

It is possible to do this in Firefox via data URIs (see also Download data url file ) and optionally via the download attribute.

See http://html5-demos.appspot.com/static/a.download.html for an HTML5 shim demo.

How to force save as dialog box in firefox besides changing headers? also covers this topic.

You can also test it by the following Firefox-tested demo.

<!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):

<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>
信仰 2024-07-26 00:00:28

execCommand 并未在浏览器之间完全标准化。 事实上, execCommand('SaveAs', ...) 似乎只在 IE 上受支持。 强制另存为的推荐方法是使用 content-disposition: 附件标头,如 http://www.jtricks.com/bits/content_disposition.html

由于这是 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>

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>
对不⑦ 2024-07-26 00:00:28

正如 Microsoft 所说,“没有适用于这个方法。”

As Microsoft puts it, "There is no public standard that applies to this method."

忆悲凉 2024-07-26 00:00:28

Firefox 不支持 execCommand。 事实上,它似乎只适用于 IE。

Firefox doesn't support execCommand. In fact it seems to be IE-only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文