gpt4 book ai didi

javascript - execCommand SaveAs 在 Firefox 中是否有效?

转载 作者:数据小太阳 更新时间:2023-10-29 04:24:35 24 4
gpt4 key购买 nike

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

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

(用作书签)

最佳答案

可以通过 data URIs 在 Firefox 中执行此操作(另请参阅 Download data url file )并可选择通过下载属性。

参见 http://html5-demos.appspot.com/static/a.download.html用于 HTML5 垫片演示。

How to force save as dialog box in firefox besides changing headers?也涵盖了这个主题。

您也可以通过以下Firefox测试的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>

以下方法也适用于 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>

关于javascript - execCommand SaveAs 在 Firefox 中是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/833015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com