从JAVASCRIPT读取txt文件

时间:2018-09-23 15:31:55

标签: javascript jquery text webpage

我正在尝试创建一个函数,该函数读取文本文件的内容,然后将其显示在新窗口中。现在,这是我的代码:

function readFile(file)
    {
        var file = new XMLHttpRequest();
        file.open("GET", file, false);
        file.onreadystatechange = function ()
        {
            if(file.readyState === 4)
            {
                if(file.status === 200 || file.status == 0)
                {
                    var Text = file.responseText;
                    myWindow.document.write (Text);
                }
            }
        }
        file.send(null);
    }

我稍后调用该函数,将文件的路由作为参数传递:

readFile("test.txt");

问题在于它显示如下:

enter image description here

我希望文件中的文本能像普通文件中那样显示,而所有信息都不会显示在顶部,且间距正确。

谢谢。

0 个答案:

没有答案
相关问题