简体   繁体   English

如何使用javascript读取/写入文本文件?

[英]How to read and write from/into text file using javascript?

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. 我尝试过的操作就像创建一个iframe并从iframe中将文本文件加载到其中一样,我读取了内容并使用字符串操作进行了一些更改,并且我不知道如何写回文本文件。 and also on ie browser this code is not working. 而且在浏览器上,此代码不起作用。

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>

In IE this is possible using ActiveXObject and HTA. 在IE中,可以使用ActiveXObject和HTA。 These, however, are recommended to use local only, not in the WEB. 但是,建议这些仅在本地使用,而不在WEB中使用。 Look at: http://msdn.microsoft.com/en-us/library/ms536471%28v=vs.85%29.aspx 查看: 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/zh-cn/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的使用。

如果您在谈论本地文件系统,那么“ HTML5是否允许您从浏览器内部与本地客户端文件进行交互”中涵盖了您的选项。

如何读取文本文件的内容并将其写入<div>使用普通的 JavaScript?</div><div id="text_translate"><p> <strong>问题:</strong>如何从文本文件中读取内容(与 html 页面一起保存在服务器上)并将该内容写入 HTML 页面上的<em>“innerHTML”</em>中?</p><p> <strong>上下文:</strong>我想创建一个用作博客的 static 网站。 但是,我希望只有一个帖子页面,每个帖子都从文本文件中读取并写入页面的主 div。 文本文件中的内容将用所需的 html 标记进行标记,以便它们在写入 div 时像 html 一样 function (但文件本身将保存为<em>.txt</em>文件)</p><p> 理想情况下,文本文件应该能够存储在与调用它的 html 页面(和/或脚本文件)不同的文件夹中。</p><p> <strong>附加信息:</strong>我遇到的大多数解决方案都与读取用户上传的文件有关。 这不是我想要理解的。 我试图了解如何从作为网站资产一部分存储的文本文件中读取数据。</p></div> - How do I read a text file's content and write it to a <div> using plain JavaScript?

暂无
暂无

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

相关问题 如何使用javascript从文本文件中读取/写入? - How to Read/Write from text file using javascript? 用JavaScript读写文本文件 - Read and write text file in JavaScript 如何使用 JavaScript 读写文件? - How to read and write into file using JavaScript? 如何在JavaScript中使用SWFUpload读写文件? - How to read and write a file using SWFUpload in javaScript? 如何在Javascript中读取和写入文件 - How to read and write from a file in Javascript 如何使用 JavaScript 从服务器读取文本文件? - How to read a text file from server using JavaScript? 如何使用具有完整路径的JavaScript从服务器读取文本文件? - How to read a text file from server using JavaScript with complete path? 在Objective-C项目中从JavaScript读取/写入文本文件 - Read/write text file from javascript in objective-c project 如何使用Javascript在文本文件中写入值 - How to write value in text file using Javascript 如何读取文本文件的内容并将其写入<div>使用普通的 JavaScript?</div><div id="text_translate"><p> <strong>问题:</strong>如何从文本文件中读取内容(与 html 页面一起保存在服务器上)并将该内容写入 HTML 页面上的<em>“innerHTML”</em>中?</p><p> <strong>上下文:</strong>我想创建一个用作博客的 static 网站。 但是,我希望只有一个帖子页面,每个帖子都从文本文件中读取并写入页面的主 div。 文本文件中的内容将用所需的 html 标记进行标记,以便它们在写入 div 时像 html 一样 function (但文件本身将保存为<em>.txt</em>文件)</p><p> 理想情况下,文本文件应该能够存储在与调用它的 html 页面(和/或脚本文件)不同的文件夹中。</p><p> <strong>附加信息:</strong>我遇到的大多数解决方案都与读取用户上传的文件有关。 这不是我想要理解的。 我试图了解如何从作为网站资产一部分存储的文本文件中读取数据。</p></div> - How do I read a text file's content and write it to a <div> using plain JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM