javascript 在html中写入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13578748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write to text file in html
提问by And_Learner
I wanted to try giving an output to a file using a Small screen on HTML. Everytime I click on the button I want the text in the file to be replaced. This is the code I wrote:
我想尝试使用 HTML 上的小屏幕为文件提供输出。每次单击按钮时,我都希望替换文件中的文本。这是我写的代码:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>This is Web App</title>
</head>
<script>
function createDoc()
{
var doc=document.open("answer.txt","replace");
var txt="<html><body>You clicked Allow</body></html>";
doc.write(txt);
doc.close();
}
function createDoc2()
{
var doc=document.open("answer.txt","replace");
var txt="<html><body>You clicked Deny</body></html>";
doc.write(txt);
doc.close();
}
</script>
<body>
<h1> This is the visitor at your house</h1>
<img src = "./images/chef.gif" width="130" height="101" />
<br />
<button name="Yes" type="button" onclick="createDoc()">Allow! </button>
<button name="No" type="button" onclick="createDoc2()">Deny! </button>
</body>
</html>
I know it is not the correct way to do it but am trying to learn. Please help me and point out my mistakes and tell me how to correct them if possible. I know there might be plenty. I am just trying to teach myself at this point. Your help is much appreciated.
我知道这不是正确的方法,但我正在努力学习。请帮助我并指出我的错误,并告诉我如何在可能的情况下纠正它们。我知道可能有很多。在这一点上,我只是想自学。非常感谢您的帮助。
采纳答案by beatgammit
If you just want to download a file generated by your page, this question may help:
如果您只想下载页面生成的文件,这个问题可能会有所帮助:
The idea is that you need to encode your data as base64 (not hard, modern browsers support btoa) and direct the browser to open it. If you trick it by giving the file a mime-type it doesn't know how to open (such as application/octet-steam
), it will download the file instead of displaying it.
这个想法是你需要将数据编码为 base64(不难,现代浏览器支持btoa)并指示浏览器打开它。如果您通过为文件提供一个它不知道如何打开的 MIME 类型(例如application/octet-steam
)来欺骗它,它将下载该文件而不是显示它。
The problem with this solution is that you cannot name the file.
此解决方案的问题是您无法命名文件。
If you want to name the file, I'd POST
the data to your webserver, write it to a file on the server, then do a window.open
with the path to the file. This will allow you to download the file in question.
如果您想命名文件,我POST
会将数据发送到您的网络服务器,将其写入服务器上的文件,然后window.open
使用该文件的路径。这将允许您下载相关文件。
There's a draft in progress to allow Javascript to write to a file directly, but this file will be in a sandboxed location that users don't have direct access to. This is only for web-apps to store large amounts of data on the client. From your question, this is likely not what you want. Try one of the above solutions.
有一个草案正在进行中,允许 Javascript 直接写入文件,但该文件将位于用户无法直接访问的沙盒位置。这仅适用于在客户端存储大量数据的 Web 应用程序。从您的问题来看,这可能不是您想要的。尝试上述解决方案之一。