简体   繁体   中英

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

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.

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 (eg 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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