如何使用JavaScript/HTML5创建.txt文件?

abc*_*abc 8 html javascript

我是javascript的新手.互联网上与使用javascript创建文本文件相关的所有代码在我的笔记本电脑中无效.任何人都可以给我我的想法或可能的代码.

Web*_*olf 7

这段代码应该可以运行,尝试一下,如果这不起作用,那么它可能是您浏览器的问题:

(function () {
var textFile = null,
  makeTextFile = function (text) {
    var data = new Blob([text], {type: 'text/plain'});

    // If we are replacing a previously generated file we need to
    // manually revoke the object URL to avoid memory leaks.
    if (textFile !== null) {
      window.URL.revokeObjectURL(textFile);
    }

    textFile = window.URL.createObjectURL(data);

    return textFile;
  };


  var create = document.getElementById('create'),
    textbox = document.getElementById('textbox');

  create.addEventListener('click', function () {
    var link = document.getElementById('downloadlink');
    link.href = makeTextFile(textbox.value);
    link.style.display = 'block';
  }, false);
})();
Run Code Online (Sandbox Code Playgroud)

和HTML:

<textarea id="textbox">Type something here</textarea> <button id="create">Create file</button> 
<a download="info.txt" id="downloadlink" style="display: none">Download</a>
Run Code Online (Sandbox Code Playgroud)

取自这个小提琴:

http://jsfiddle.net/uselesscode/qm5ag/