javascript 在 HTML 脚本中实现 FileSaver.js saveAs() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30947247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing FileSaver.js saveAs() function into HTML script
提问by MasterChef
Right now I am attempting to save information from an HTML page that I am creating to a text document, using javascript and the FileSaver.js package. I am not very well versed in HTML and am completely new to javascript, so odds are that I am making some egregious mistake. At the moment, I have eligrey's FileSaver.jsfile in the same directory as the HTML file that I am working from, so I should be able to call it simply as "FileSaver.js" in HTML.
现在,我正在尝试使用 javascript 和 FileSaver.js 包将信息从我正在创建的 HTML 页面保存到文本文档。我不太精通 HTML 并且对 javascript 完全陌生,所以很可能我犯了一些严重的错误。目前,我将 eligrey 的FileSaver.js文件与我正在使用的 HTML 文件位于同一目录中,因此我应该可以在 HTML 中将其简单地称为“FileSaver.js”。
Right now I am working from something like this:
现在我的工作是这样的:
//some irrelevant text
<script src="FileSaver.js">
function download(){
//alert("hello world");
//this alert line was to test the function call
//the alert actually appears when the <script> tag
// has no src field labeled. Just an observation.
var blob = new Blob(["Hello World"],{type:"text/plain;charset=utf-8"});
saveAs(blob,"helloworld.txt");
}
</script>
//some more irrelevant text
<input type="button" value="download" onclick="download();"/>
/*from this button I want to gather information from input text fields on the page.
I already know how to do that, the problem is creating and, subsequently,
downloading the text file I am trying to create. For simplicity's sake, the text I am
capturing in this example is hardcoded as "hello world"*/
//last of irrelevant text
//oh, and don't try to run this snippet :P
I also have eligrey's Blob.js file available in my immediate working directory as well, just in case.
我的直接工作目录中也有 eligrey 的 Blob.js 文件,以防万一。
As of now, the button does nothing. I am fairly sure that I am calling the function correctly considering I could receive the javascript alert text, at least when the script tag had no src. If I do add a src, absolutely nothing happens. The browser that I am testing from is the latest Google Chrome stable build (43.0 I think) on Windows XP. Please inform me of anything that I am missing and/or doing wrong. Thanks in advance
截至目前,该按钮什么都不做。我相当确定我正确地调用了该函数,因为我可以收到 javascript 警报文本,至少在脚本标签没有 src 时是这样。如果我确实添加了 src,则绝对不会发生任何事情。我正在测试的浏览器是 Windows XP 上最新的 Google Chrome 稳定版本(我认为是 43.0)。请告诉我任何我遗漏和/或做错的事情。提前致谢
采纳答案by Juan Mendes
You cannot have a script tag with a src
attribute and content inside the tag. Split it into two separate script tags. See What if script tag has both "src" and inline script?
您不能在标签内拥有带有src
属性和内容的脚本标签。将其拆分为两个单独的脚本标记。看看如果脚本标签有“src”和内联脚本怎么办?
Note that <script>
cannot be a self-closing tag
注意<script>
不能是自闭合标签
<script src="FileSaver.js"></script>
<script>
function download(){
//alert("hello world");
//this alert line was to test the function call
//the alert actually appears when the <script> tag
// has no src field labeled. Just an observation.
var blob = new Blob(["Hello World"],{type:"text/plain;charset=utf-8"});
saveAs(blob,"helloworld.txt");
}
</script>