简体   繁体   English

如何在 HTML/JS 中将 txt 文件保存在服务器上?

[英]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. web 浏览器能够写入服务器上的任意文件将是一个严重的安全风险。

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 ).相反,创建一个 web 服务(使用您选择的服务器端编程语言)并向其发出 HTTP 请求(例如,通过提交表单或使用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).请注意,对于注册系统,您几乎肯定会希望将数据保存到数据库而不是文件中(尽管这仍然是服务器端代码的问题)。

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

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