2012-07-27 122 views
0



我有一个使用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: &nbsp; 
      <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> 
+0

你可以从[手写]文件中读取换行符吗? – Bergi 2012-07-27 19:28:07

+1

你有没有尝试windows linebreaks:'\ r \ n'? – Bergi 2012-07-27 19:28:34

+0

啊,我不知道!哈,这比我想要做的要容易得多。是的,'\ r \ n'工作,以及'\ r'之后的任何附加转义序列,例如:'\ r \ n \ t \ t'。感谢@Bergi的帮助,回答这个问题,我会将其标记为最佳。 ;) – 2012-07-27 19:45:46

回答

2

视窗预计"\r\n"作为换行符。我很确定你会在你的textarea的value中找到它们(在进入后)。当您使用"\n"设置一个值时,它们会自动插入,并且大多数库(如jQuery)在读取值时都会用“正常”换行符替换它们。

但是,我期望一个只有"\n"的文件可读/写,并且当你将文件的文本加载到你的textarea时,它们应该显示出来。 MS记事本可能有问题显示它们。

+0

啊,确切地说 - 需要使用我的文本文件和记事本,所以我一直在用它来测试它...正如你所说的那样''n'*没有* windows line-打破“'\ r \ n'”。感谢Bergi的帮助。 – 2012-07-27 21:21:23

+0

使用记事本++(免费),您可以设置和更改linebreaks :-) – Bergi 2012-07-27 21:29:54

相关问题