gpt4 book ai didi

javascript - 如何在 iOS Safari 上使用 JavaScript 下载 JSON 文件

转载 作者:行者123 更新时间:2023-12-01 15:10:07 33 4
gpt4 key购买 nike

我阅读了很多关于这个主题的内容,Safari 似乎对此有疑问(甚至是 filesaver.js)。我仍然想知道你们是否有一种方法可以让用户单击按钮并将带有文件名的 JSON 文件下载到他的设备。
那里有很多相关的线程;并且,Safari 过去似乎遇到过已解决的问题。但目前的 Safari 版本似乎仍然无法做到这一点。我把最后的希望寄托在你们身上。对最新版本的 iOS 更新没有帮助。
这是我的方法,适用于 Safari 桌面:

    exportButton.addEventListener("click", () => {
const appState = databaseConnector.getApplicationStateAsString();
const blob = new Blob([appState], { type: "text/json" });
const fileName = `backup_${
new Date().toISOString().split("T")[0]
}.json`;

const tempElement = document.createElement("a");
const url = URL.createObjectURL(blob);
tempElement.href = url;
tempElement.download = fileName;
document.body.appendChild(tempElement);
tempElement.click();

setTimeout(function () {
document.body.removeChild(tempElement);
window.URL.revokeObjectURL(url);
});
});

最佳答案

试试下面的代码。

<!DOCTYPE html>
<title>Answer</title>
<a id=a download=answer.json href style=-webkit-appearance:button;color:black;text-decoration:none>Download</a>
<script>
a.href = "data:text/json;," + JSON.stringify({name: "answer"})
</script>

关于javascript - 如何在 iOS Safari 上使用 JavaScript 下载 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62378577/

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