我有一個使用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