【发布时间】:2018-05-02 01:39:58
【问题描述】:
我在 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 || (功能(视图){
-lol.html:15 Uncaught ReferenceError: saveAs is not defined on this line code inside the html file saveAs(blob, "hello world.txt");}
【问题讨论】:
-
在他们的网站示例中,他们使用了您不包括在内的 Blob.js....
-
@DanielTate 我添加了我在演示中看到的所有脚本,但它仍然无法正常工作。
-
@Kaiido 我添加了我遇到的错误,我正在使用 Opera
-
您的代码 100% 有效jsfiddle.net/1pr4090t
-
@DanielTate 这很奇怪它对我不起作用。
标签: javascript html