使用ajax从文件中读取

时间:2022-04-08 10:07:37

I am trying to read from a file on the local machine using Ajax.The file it is reading from is a text file having numbers like 1 2 3 4 5 but when I run the code in a browser in spite of all the formatting the numbers appear separated by a space rather then on new lines.If someone could tell me how to get the numbers printed out in the format that they originally exist,I would be thankful

我正在尝试使用Ajax读取本地计算机上的文件。它正在读取的文件是一个文本文件,其数字类似于1 2 3 4 5但是当我在浏览器中运行代码时,尽管所有格式化数字如果有人能告诉我如何以原来存在的格式打印出数字,我会感激不尽

Here is the code:

这是代码:

<html>
<script>
var oRequest;
var i;
if(document.all) {

oRequest = new ActiveXObject("Microsoft.XMLHTTP")
}
else {
oRequest = new XMLHttpRequest();
}
oRequest.open("GET", "file:///C:/testing.txt", false);
oRequest.send(null);
textToBeWritten = oRequest.responseText;
document.write(textToBeWritten+"<br />");
</script>
</html>

1 个解决方案

#1


0  

You need to convert text-file newlines (\n) to HTML newlines (<br />) like so:

您需要将文本文件换行符(\ n)转换为HTML换行符(
),如下所示:

textToBeWritten = oRequest.responseText.replace("\n", "<br />");

#1


0  

You need to convert text-file newlines (\n) to HTML newlines (<br />) like so:

您需要将文本文件换行符(\ n)转换为HTML换行符(
),如下所示:

textToBeWritten = oRequest.responseText.replace("\n", "<br />");