javascript 如何使用javascript从/写入文本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9410992/
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 read and write from/into text file using javascript?
提问by Kunal Vashist
Do any one have a sample code of how to read and write from/into text file using javascript
有没有人有如何使用javascript从/向文本文件读取和写入的示例代码
What i tried is like created a iframe and load text file into it from iframe i read the content and using string operation make some changes, and i have no idea how to write back to text file. and also on ie browser this code is not working.
我尝试的是创建一个 iframe 并将文本文件从 iframe 加载到其中我读取内容并使用字符串操作进行一些更改,但我不知道如何写回文本文件。并且在 IE 浏览器上,此代码不起作用。
My text.txt file contain First line second Line Third Line Fourth Line
我的 text.txt 文件包含第一行第二行第三行第四行
<html>
<head>
<title></title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var srcFrame;
function loadOuter(doc) {
srcFrame = document.getElementById("hiddenContent");
srcFrame.src = doc;
transferHTML();
}
function transferHTML(){
srcContent='';
if (srcFrame.contentDocument){
alert("document");
srcContent=srcFrame.contentDocument.getElementsByTagName("BODY")[0].innerHTML;
}
else if (srcFrame.contentWindow){
alert("window");
srcContent=srcFrame.contentWindow.document.body.innerHTML;
}
srcContent.length;
alert(" before push "+srcContent);
var arrayText="Last Line";
var lines = srcContent.split('\n');
lines=lines.slice(0, -1);
lines.push(arrayText,"</pre>");
lines = lines.join('\n');
srcContent=lines;
alert(srcContent);
document.getElementById("outerDisplay").innerHTML = srcContent;
}
</script>
<INPUT TYPE="button" VALUE="Test.txt" onClick="loadOuter('Test.txt')" >
<div id="outerDisplay"></div>
<iframe id="hiddenContent" width="200" height="200" style="position:absolute;visibility:hidden;" ></iframe>
</body>
</html>
采纳答案by Teemu
In IE this is possible using ActiveXObject and HTA. These, however, are recommended to use local only, not in the WEB. Look at: http://msdn.microsoft.com/en-us/library/ms536471%28v=vs.85%29.aspx
在 IE 中,这可以使用 ActiveXObject 和 HTA。但是,建议仅在本地使用这些,而不是在 WEB 中使用。看:http: //msdn.microsoft.com/en-us/library/ms536471%28v=vs.85%29.aspx
More info for file operations: http://msdn.microsoft.com/en-us/library/bstcxhf7%28v=vs.84%29.aspx
有关文件操作的更多信息:http: //msdn.microsoft.com/en-us/library/bstcxhf7%28v=vs.84%29.aspx
Basic functions below:
基本功能如下:
ActiveXObject definition:
ActiveXObject 定义:
fso=new ActiveXObject('Scripting.FileSystemObject');
Reading file:
读取文件:
iStream=fso.OpenTextFile('filePath',1,false);
iStream.ReadAll();
/* or looped iStream.ReadLine() */
iStream.Close();
Writing file:
写入文件:
oStream=fso.OpenTextFile('filePath',2,true);
oStream.WriteLine(/*your_data*/);// This usually is looped according to your data
oStream.Close();
fso
-object can also be used in regular HTM-page, but you're asked to accept use of the ActiveXObject very often.
fso
-object 也可以在常规 HTM 页面中使用,但是您需要经常接受 ActiveXObject 的使用。
回答by Wayne Cornish
If you're talking about the local file system, your options are covered in Does HTML5 allow you to interact with local client files from within a browser
如果您在谈论本地文件系统,您的选项包含在HTML5 是否允许您从浏览器中与本地客户端文件交互