简体   繁体   English

在 HTML 脚本中实现 FileSaver.js saveAs() 函数

[英]Implementing FileSaver.js saveAs() function into HTML script

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.现在我正在尝试使用 javascript 和 FileSaver.js 包将信息从我创建的 HTML 页面保存到文本文档。 I am not very well versed in HTML and am completely new to javascript, so odds are that I am making some egregious mistake.我不太精通 HTML 并且对 javascript 完全陌生,所以很可能我犯了一些严重的错误。 At the moment, I have eligrey's FileSaver.js file 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.目前,我在与我正在使用的 HTML 文件相同的目录中有 eligrey 的FileSaver.js文件,所以我应该可以在 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.我相当确定我正确地调用了该函数,因为我可以收到 javascript 警报文本,至少在脚本标签没有 src 时是这样。 If I do add a src, absolutely nothing happens.如果我确实添加了一个 src,绝对不会发生任何事情。 The browser that I am testing from is the latest Google Chrome stable build (43.0 I think) on Windows XP.我正在测试的浏览器是 Windows XP 上最新的 Google Chrome 稳定版本(我认为是 43.0)。 Please inform me of anything that I am missing and/or doing wrong.请告诉我任何我遗漏和/或做错的事情。 Thanks in advance提前致谢

You cannot have a script tag with a src attribute and content inside the tag.您不能拥有带有src属性和标签内的内容的脚本标签。 Split it into two separate script tags.将其拆分为两个单独的脚本标记。 See What if script tag has both "src" and inline script?看看如果脚本标签有“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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM