使用 Jquery $.get() 逐行检索文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6535882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:01:00  来源:igfitidea点击:

Retrieve text file line-by-line using Jquery $.get()

jqueryajaxcross-domain

提问by karq

Is it possible to retrieve a txt files content line by line?

是否可以逐行检索 txt 文件内容?

Right now I'm using this code:

现在我正在使用此代码:

var file = "http://plapla.com/pla.txt";
function getFile(){
     $.get(file,function(txt){
        save(txt.responseText);
 }); 
}

the save function saves the content into a variable. And after I print it out all the content of the retrived file is in the end of each other(not line by line).

save 函数将内容保存到一个变量中。在我打印出来之后,检索到的文件的所有内容都在彼此的末尾(不是逐行)。

回答by loganfsmyth

I'm not sure if you mean that when you print out the file that it gets all pushed onto one line, or if you are saying that you want to divide the file by line.

我不确定您的意思是当您打印出文件时将其全部推到一行上,还是您说要逐行分割文件。

If the problem is that when you display it, it gets pushed together, that is just because you are putting it into an HTML page, which means that newlines are ignored. You have two options then, either replace all of the newlines with <br />tags, or put all of your text in a pre tag.

如果问题是当您显示它时,它会被推到一起,那只是因为您将它放入一个 HTML 页面,这意味着换行符被忽略。那么您有两个选择,要么用<br />标签替换所有换行符,要么将所有文本放在 pre 标签中。

var file = "http://plapla.com/pla.txt";
function getFile(){
    $.get(file,function(txt){
        save(txt.responseText.replace(/\n/g, "<br />");
    }); 
}

// OR

var file = "http://plapla.com/pla.txt";
function getFile(){
    $.get(file,function(txt){
        save($("<pre />").text(txt.responseText));
    }); 
}

If you just want to work with the data line by line, then once you have received it, you can do whatever you like with it, just as splitting in up by line.

如果你只是想逐行处理数据,那么一旦你收到它,你可以随心所欲地处理它,就像逐行拆分一样。

var file = "http://plapla.com/pla.txt";
function getFile(){
    $.get(file,function(txt){
        var lines = txt.responseText.split("\n");
        for (var i = 0, len = lines.length; i < len; i++) {
            save(lines[i]);
        }
    }); 
}

回答by Mustafa

Javascript can only make a HTTP request, which would return you a HTTP response with the Full Text, do not get the idea of reading line-by-line with opening a socket and reading line-by-line.

Javascript 只能发出一个 HTTP 请求,它会返回一个带有全文的 HTTP 响应,不要通过打开套接字和逐行读取逐行读取的想法。

However, if you mean treating the already read data line-by-line, you can do the following:

但是,如果您的意思是逐行处理已读取的数据,则可以执行以下操作:

lineByLine = readData.split("\n");
process(lineByLine[0]); // First line

回答by Greg Pettit

If you're trying to display line-by-line (preserving encoded line breaks), the simplest way to do it is to push your retrieved text into a textarea element instead of into a div (or similar). The browser intelligently handles the incoming text appropriately.

如果您尝试逐行显示(保留编码的换行符),最简单的方法是将检索到的文本推送到 textarea 元素而不是 div (或类似元素)。浏览器会智能地适当处理传入的文本。

Self-update: noticed that neither technique (data.replace() or just using a textarea) seems to work in IE9.

自我更新:注意到这两种技术(data.replace() 或仅使用 textarea)似乎都不适用于 IE9。