【问题标题】:How do I convert escape characters in Javascript to a .txt file?如何将 Javascript 中的转义字符转换为 .txt 文件?
【发布时间】:2012-07-28 03:53:42
【问题描述】:



我有一个 HTML 文件,它使用 Javascript 通过 ActiveXObject 对 .txt 文件执行文件 I/O 操作(适用于 Windows 操作系统的 Internet Explorer) .

HTML页面上有一个文本输入框,还有一个按钮。该按钮调用函数 onclick 将输入的文本写入 .txt 文件的末尾。 HTML页面上还有一个textarea,将.txt文件修改后的内容复制粘贴到其中。到目前为止,所有这些都有效......


所以,我想用 Javascript 从我的 HTML 页面中将制表符和换行符插入到 .txt 文件中。我正在使用这一行将 .txt 文件内容复制到 textarea 中,并在变量中初始化:
var newText = oldText + "\n" + document.getElementById("userInput").value;

当然,转义字符 \n 适用于 HTML 页面,而不适用于 .txt 文件...


那么如何将新行和制表符编码为 .txt 文件的可解析格式?我曾尝试在ANSIfound hereASCIIfound here 上使用escape() 方法,但没有运气。

到目前为止,这是我的代码:
<!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>

【问题讨论】:

  • 你能从[手写]文件中读取换行符吗?
  • 你试过windows换行符吗:\r\n?
  • 啊,我不知道!哈,这比我尝试做的要容易得多。是的,\r\n 有效,\r 之后的任何其他转义序列也有效,例如:\r\n\t\t。感谢@Bergi 的帮助,回答问题,我会将其标记为最佳。 ;)

标签: javascript escaping text-files activexobject


【解决方案1】:

Windows 期望 "\r\n" 作为换行符。我很确定您也会在您的 textarea 的value 中找到它们(按回车后)。当您使用 "\n" 设置值时,它们会自动插入,并且大多数库(如 jQuery)在读取值时会用“正常”换行符替换它们。

但是,我希望只有"\n" 的文件读/写可以工作,并且当您将文件的文本加载到文本区域时,它们应该会显示出来。 MS 记事本可能无法显示它们。

【讨论】:

  • 啊,确切地说——需要将我的文本文件与记事本一起使用,所以我一直在用它来测试它......正如你所说的那样,\n 有问题没有 windows换行符“\r\n”。感谢贝尔吉的帮助。
  • 使用 Notepad++(免费),在那里你可以设置和更改换行符 :-)
猜你喜欢
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
相关资源
最近更新 更多