2012-07-27 123 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

相關問題