如何使用javascript在任何驱动器中创建文本文件

如何使用javascript在任何驱动器中创建文本文件,javascript,Javascript,我已经尝试过这段代码,但这段代码没有创建文本文件。提交后,它只是显示下载选项,但我想在任何驱动器中存储一个文本文件在Javascript中,不可能通过浏览器将文件保存到主机PC上的任何位置,因为这将给客户端带来巨大的安全风险。您可以要求他们下载该文件,也可以使用window.localStorage如果您的沙盒浏览器可以在系统的任何位置写入文件,那就太好了。我认为它不能也不应该。 <textarea id="textbox">Type something here</texta

我已经尝试过这段代码,但这段代码没有创建文本文件。提交后,它只是显示下载选项,但我想在任何驱动器中存储一个文本文件

在Javascript中,不可能通过浏览器将文件保存到主机PC上的任何位置,因为这将给客户端带来巨大的安全风险。您可以要求他们下载该文件,也可以使用window.localStorage

如果您的沙盒浏览器可以在系统的任何位置写入文件,那就太好了。我认为它不能也不应该。
<textarea id="textbox">Type something here</textarea> <button id="create">Create file</button> <a download="info.txt" id="downloadlink" style="display: none">Download</a>

<script>
(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);
})();
</script>