-1

I'm trying to create a site where the user input's some data (string) into an input box or form, and then that information is put into a .txt file in the user’a C drive. Anyone have any code that doesn't involve uploading it to a server first, or is that my only option?

I don’t currently have access to my code at the moment, but I’ve been trying multiple forms and object creations etc but with no luck so far.

  • Textbox and POST or GET? – Andreas Nov 28 '18 at 17:44
  • 1
    It is unclear what you are asking. Where are you wanting to save the text file? The user/web browser's computer or the server? Also, if you are asking for code without showing what you've done and tried, please read https://stackoverflow.com/help/how-to-ask and modify your post. – Stephen M -on strike- Nov 28 '18 at 19:18
  • Possible duplicate of [Create a file in memory for user to download, not through server](https://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server) – Herohtar Nov 28 '18 at 23:02
  • OP, you need to engage with those trying to help, not just repost if you dont get the answer you want. its a dialogue, not a monologue. –  Nov 28 '18 at 23:07

1 Answers1

1

It's hard to fully understand what you're trying to do, but something like this should be able to store text from an input into a Blob, and then allow you to download it as a .txt file without any upload:

    function createFile() {
      var a = document.getElementById("download");
      var text = document.getElementById('text').value
      var file = new Blob([text], {type: 'text/plain'});
      a.href = URL.createObjectURL(file);
      a.download = 'myFile.txt';
    }

document.getElementById('create').addEventListener('click', createFile);

HTML:

<input id="text" type="textbox">
<button id="create">create file</button>
<a id="download"> download file </a>

Working codepen

You should be able to easily edit the above to take in text from anywhere or name the file as needed, but I think the key from the example is just leveraging the Blob API

Pabs123
  • 3,385
  • 13
  • 29