Home > Enterprise >  How to to save txt file on server in HTML/JS?
How to to save txt file on server in HTML/JS?

Time:12-14

I'm making signup form stuff and I want to save data to server and I got this code :

function Signup()
   {
     var text = "hello world",
   blob = new Blob([text], { type: 'text/plain' }),
   anchor = document.createElement('a');

anchor.download = "hello.txt";
anchor
anchor.href = (window.webkitURL || window.URL).createObjectURL(blob);
anchor.dataset.downloadurl = ['text/plain', anchor.download, anchor.href].join(':');
anchor.click();
   }

But its download file and I'm wondering how to save/download it to server.

CodePudding user response:

You can't.

The code you have found is for triggering a download and saving a file to the browser's download directory (client-side).

It would be a serious security risk for a web browser to be able to write to arbitrary files on the server.

Instead, create a web service (using the server side programming language of your choice) and make an HTTP request to it (e.g. by submitting a form or using fetch).

Note that for a sign up system, you are almost certainly going to want to save the data to a database and not to a file (that is still a matter for server-side code though).

  • Related