如何使用 HTML 中的 javascript 打开 html 中的文本并将其保存到文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3223915/
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
How to open and save text in html to a file using javascript in HTML
提问by subanki
i have a textarea and two buttons
我有一个 textarea 和两个按钮
like
喜欢
<form name="form1">
<textarea name="text1"> HTML Codes goes here </textarea>
<input type="button"> Open File
<input type="button"> Save File
</form>
when i click on "save" button i want the text in textarea to be saved (i want it to pop up the "save as" dialog box)
当我单击“保存”按钮时,我希望保存 textarea 中的文本(我希望它弹出“另存为”对话框)
When i click on "open" , it should allow me to choose any html or textfile... and load the text in the textfile/htmlcode into my textarea.
当我点击“打开”时,它应该允许我选择任何 html 或文本文件......并将文本文件/htmlcode 中的文本加载到我的文本区域中。
Found this code in http://www.dynamicdrive.com/forums/archive/index.php/t-10532.html
在http://www.dynamicdrive.com/forums/archive/index.php/t-10532.html 中找到此代码
<html>
<head>
</head>
<body>
<script language="javascript">
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\NewFile.txt", true);
var text=document.getElementById("TextArea1").innerText;
s.WriteLine(text);
s.WriteLine('***********************');
s.Close();
}
</script>
<form name="abc">
<textarea name="text">FIFA</textarea>
<button onclick="WriteToFile()">Click to save</Button>
</form>
</body>
</html>
this would work if it porvides the user the choice to save the file ...and i forgot to say that all files are in the client computer.
如果它允许用户选择保存文件,这将起作用......我忘了说所有文件都在客户端计算机中。
Thanx in Advance
提前致谢
-Miss Subanki
-苏班基小姐
采纳答案by JC Leyba
You can save a file with Javascript, but you have to use execcommand and then you'd be limited to Internet Explorer.
您可以使用 Javascript 保存文件,但您必须使用 execcommand,然后您将仅限于 Internet Explorer。
document.execCommand('SaveAs', true);
回答by Christian
Saving - You have to do that server-side, but it isn't difficult; in PHP you would just force some HTTP headers before outputting the data:
保存 - 您必须在服务器端执行该操作,但这并不困难;在 PHP 中,您只需在输出数据之前强制使用一些 HTTP 标头:
// set the content type
header('Content-type: text/plain');
// force save as dialog (and suggest filename)
header('Content-Disposition: attachment; filename="download.txt"');
// next echo the text
echo $_POST['text'];
Opening - You have to handle the uploaded data server-side, unless you use some proprietary (albeit "open") API like in firefox.
开放 - 您必须在服务器端处理上传的数据,除非您使用某些专有(尽管是“开放”)API,例如在 Firefox 中。
回答by JC Leyba
This is possible only in IE, since the ActiveXObjecthas access to files (Read/Write). so, you can do it
这仅在IE 中是可能的,因为它ActiveXObject可以访问文件(读/写)。所以,你可以做到
I still remember, I tried it once and got succeeded
我还记得,我试过一次就成功了
But Remember it only works in IE
但请记住它仅适用于 IE

