简体   繁体   English

如何从html获取输入值到文本文件

[英]How to get input values from html to text file

I have tried so many examples but none of them works t我尝试了很多例子,但没有一个有效

 (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); })();
 <textarea id="textbox">Type something here</textarea> <button id="create">Create file</button> <a download="info.txt" id="downloadlink" style="display: none">Download</a>

here is a code which is working good but i need to download automatically without using link is it possible?这是一个运行良好的代码,但我需要在不使用链接的情况下自动下载,这可能吗?

You could use the following script to create and save automatically a file from the browser to your operating system.您可以使用以下脚本从浏览器自动创建文件并将其保存到操作系统。 This code works only on latest version of Chrome.此代码仅适用于最新版本的 Chrome。

What the script does?脚本是做什么的?

  • It creates a temporary URL containing the specified File object or Blob object - Programmatically click the link just created so the file will be download by the browser.它会创建一个包含指定 File 对象或 Blob 对象的临时 URL - 以编程方式单击刚刚创建的链接,以便浏览器下载该文件。
  • Immediately after remove the link from the page.从页面中删除链接后立即。

     var saveDataToFile = function (data, fileName, properties) { window.URL = window.URL || window.webkitURL; var file = new File(data, fileName, properties), link = document.createElement('a'); link.href = window.URL.createObjectURL(file); link.download = fileName; document.body.appendChild(link); link.click(); var timer = setTimeout(function () { window.URL.revokeObjectURL(link.href); document.body.removeChild(link); clearTimeout(timer); }, 100); };

If you deconstruct this problem, there's a few key points:如果你解构这个问题,有几个关键点:

  1. Initially, when the user hasn't typed text into the textarea, the button should not be visible.最初,当用户没有在文本区域中输入文本时,按钮不应该是可见的。 (I may be wrong here though) (虽然我可能在这里错了)
  2. When the user starts typing, the button has to appear.当用户开始输入时,按钮必须出现。
  3. Whatever is inside the textarea after that, has to be downloadable per click on the button.之后文本区域内的任何内容都必须每次单击按钮都可以下载。

So, it's a matter of two event listeners.所以,这是两个事件监听器的问题。

The first one is "focus": when the textarea received focus, its value is an empty string, and the button appears.第一个是“focus”:当textarea收到焦点时,其值为空字符串,按钮出现。 The user hasn't yet started typing, but there's actually no need to force them to.用户还没有开始输入,但实际上没有必要强迫他们输入。

The second one is "change": per every change in the field, we need to update the value of href attribute of the link, so that when the user clicks that element, file download happens, and the content is precisely what's inside the textarea.第二个是“改变”:每一个字段的改变,我们需要更新链接的href属性的值,这样当用户点击那个元素时,就会发生文件下载,并且内容就是textarea里面的内容. Good thing, a function passed to "change" event listener is executed with the first argument instance of Event, which means you can do event.target.value to get the new value per every change.好东西,传递给“更改”事件侦听器的函数使用 Event 的第一个参数实例执行,这意味着您可以执行event.target.value来获取每次更改的新值。 It means, the whole text from within textarea.这意味着,文本区域内的整个文本。

Summing up, it's总结就是

<textarea id="textbox" placeholder="Type something here"></textarea>
<a download="info.txt" id="create" href="#" style="display: none;">Create file</a>

and

(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');
  var textbox = document.getElementById('textbox');

  textbox.addEventListener('focus', function (event) {
    create.style.display = 'block';
    create.href = makeTextFile(''); // initially, the text is empty.
  });

  textbox.addEventListener('change', function (event) {
    create.href = makeTextFile(event.target.value); // per every change, update value of href attribute of #create
  });
})();

Take note that only a element can have href assigned with Blob value.请注意,只有a元素才能为href分配 Blob 值。 Using a button element would be a little bit more complicated, so it might be easier to just make the a element look like a button.使用button元素会稍微复杂一些,所以让a元素看起来像一个按钮可能会更容易。

See the Codepen to make sure it works as you expect, or feel free to edit it otherwise.查看Codepen以确保它按预期工作,或者随意编辑它。

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

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