gpt4 book ai didi

javascript - 如何使用 FileSaver.js 保存文本文件

转载 作者:行者123 更新时间:2023-12-02 20:25:22 26 4
gpt4 key购买 nike

我在 github 上下载了 FilSaver.js 并尝试对其进行测试以备将来使用。我制作了一个简单的 html 文件,它接受输入,当单击按钮时触发一个函数,该函数将使用 FileSaver 保存文件.js.

在我的文件夹中只有 FileSaver.js 和 HTML 文件。

这是我的代码。

<html>
<head>

</head>
<body>
<input type="text" id="sample">
<button id="button" onclick="download();">save as</button>

<script src="FileSaver.js"></script>
<script type="text/javascript">
function download(){
var save = document.getElementById("sample").value;
alert(save);
var blob = new Blob([save], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");}
</script>
</body>
</html>

错误:

-Uncaught SyntaxError: Unexpected token export on this line of code inside the FileSaver.js var saveAs = saveAs || (function(view) {

-lol.html:15 Uncaught ReferenceError: saveAs is not defined on this line of code inside the html file saveAs(blob, "hello world.txt");}

最佳答案

只需更新到以下内容即可,试试下面演示中用于文件保护程序的 CDN 服务器

function download() {
var save = document.getElementById("sample").value;
var blob = new Blob([save], {
type: "text/plain;charset=utf-8"
});
saveAs(blob, "sample-file.txt");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/eligrey/FileSaver.js/5ed507ef8aa53d8ecfea96d96bc7214cd2476fd2/FileSaver.min.js"></script>
<input type="text" id="sample">
<button id="button" onclick="download();">save as</button>

关于javascript - 如何使用 FileSaver.js 保存文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50126062/

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