我有一个使用JavaScript做文件上的.txt文件I/O操作的HTML文件,通过的ActiveXObject(只在Internet Explorer中的作品,在Windows OS)。
在HTML页面上有一个文本输入框和一个按钮。该按钮调用函数 onclick
将输入的文本写入到.txt文件的末尾。 HTML页面上还有一个textarea,其中.txt文件的修改内容被复制并粘贴到其中。所有这些工作到目前为止...所以,我想插入标签和换行符到.txt文件中,从我的HTML页面使用Javascript。我使用该行的.txt文件内容复制到文本区域,在一个变量初始化:
如何将Javascript中的转义字符转换为.txt文件?
var newText = oldText + "\n" + document.getElementById("userInput").value;
当然,转义字符 \n
作品中的HTML页面上,而不是在。 txt文件...
那么,如何将新行和制表符编码为.txt文件的可解析格式呢?我已经使用
escape()
方法上
ANSI
值
found here尝试,并在
ASCII
值
found here,但没有运气。
这是到目前为止我的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
</head>
<body>
<p>
Enter some text here:
<input type = "text" id = "userInput" />
</p>
<input type = "button" value = "submit" onclick = "main();" />
<br />
<hr />
<br /><br /><br />
<textarea id = "textHere" rows = 25 cols = 150></textarea>
<script type = "text/javascript">
// executes all code from this function to prevent global variables
function main()
{
var filePath = getThisFilePath();
var fileText = readFile(filePath);
writeFile(filePath, fileText);
} // end of function main
function getThisFilePath()
{
var path = document.location.pathname;
// getting rid of the first forward-slash, and ending at the last forward-slash to get rid of file-name
var correctPath = path.substr(1, path.lastIndexOf("/"));
var fixedPath = correctPath.replace(/%20/gi, " "); // replacing all space entities
return fixedPath;
} // end of function getThisFilePath
function readFile(folder)
{
var fso = "";
var ots = "";
var oldText = "";
try
{
fso = new ActiveXObject("Scripting.FileSystemObject");
// in the same folder as this HTML file, in "read" mode (1)
ots = fso.OpenTextFile(folder + "writeToText.txt", 1, true);
oldText = ots.ReadAll();
ots = null;
fso = null;
}
catch(e)
{
alert("There is an error in this code!\n\tError: " + e.message);
exit(); // end the program if there is an error
}
return oldText;
} // end of function readFile
function writeFile(folder, oldText)
{
var fso = "";
var ots = "";
var newText = oldText + "\n" + document.getElementById("userInput").value;
try
{
fso = new ActiveXObject("Scripting.FileSystemObject");
// in the same folder as this HTML file, in "write" mode (2)
ots = fso.OpenTextFile(folder + "writeToText.txt", 2, true);
ots.Write(newText);
ots.Close();
ots = null;
fso = null;
}
catch(e)
{
alert("There is an error in this code!\n\tError: " + e.message);
exit(); // end the program if there is an error
}
setText(newText); // with the function below
} // end of function writeFile
// called from the function writeFile
function setText(textFile)
{
document.getElementById("textHere").value = textFile;
} // end of function setText
</script> <!-- end of javascript -->
</body>
</html>
你可以从[手写]文件中读取换行符吗? – Bergi 2012-07-27 19:28:07
你有没有尝试windows linebreaks:'\ r \ n'? – Bergi 2012-07-27 19:28:34
啊,我不知道!哈,这比我想要做的要容易得多。是的,'\ r \ n'工作,以及'\ r'之后的任何附加转义序列,例如:'\ r \ n \ t \ t'。感谢@Bergi的帮助,回答这个问题,我会将其标记为最佳。 ;) – 2012-07-27 19:45:46