使用jQuery加载大文本文件的最佳做法get() [英] Best practice for loading a large text file using jQuery get()

查看:121
本文介绍了使用jQuery加载大文本文件的最佳做法get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我将get的结果存储在一个字符串中,因为我打开的文件是3MB到20MB的纯文本文件。

然后我解析这个字符串并对其进行修改,以便最终结果可以以html格式输出。



我只是在寻找一个完整的检查来查看以这种方式加载最好的方式吗?

另外,有没有办法加载目标文本文件的块,解析块,请求另一个块等。有点像音乐播放器在播放歌曲时缓冲一首歌曲。



谢谢

是否有加载目标文本文件块的方法,解析块,请求另一个块等。

要检索资源的一部分(或块),请使用 HTTP范围标题。大多数Web服务器都正确地承认此标头



示例HTTP请求:

  GET / resource / url / path HTTP / 1.1 \r\\\

User-agent:whatever\r\\\

Host:www.example.com\r\\\

范围:bytes = 0 -3200 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ strong>



使用jQuery v1.5及更高版本,您可以告诉jQuery为单个ajax调用发送额外的HTTP标头,如下所示:

  $ .ajax({type:GET,
url:url,
headers:{Range:'bytes = 0-3200'},
....

如果你不想要修改每个 $。ajax()调用,Stofke说你可以使用beforeSendfn。例如:

  $ .ajaxSetup({beforeSend:function(xhr){
xhr.setRequestHeader(Range,bytes = 0-3200);
} });

使用 $。ajaxSetup(),你' d需要修改每个额外呼叫的范围标题。

我不知道一种告诉jQuery自动加载资源的方法。我想你必须在ajax调用的success()中使用setTimeout()或其他方法来做到这一点。每次获得响应时,请调用setTimeout()并进行下一次ajax调用。您的浏览器驻留的Javascript将需要跟踪您已经检索的文件的哪些部分,以及您想要下一个部分(字节索引)。

另一种做法是只在应用程序准备好时才调用ajax调用。在完成处理第一个范围检索的结果之后,不要等待ajax调用的成功(),而只需进行ajax调用。



另外:为了支持算术目的,在做第一次GET之前,可以使用HEAD调用来了解资源的长度,这会告诉你最大索引在你可以使用的范围内。

Currently I'm storing the results of the get in a string since the file I am opening is plain text file 3MB to 20MB in size.

Then I parse this string and modify it so that the end result can be outputted in html format.

I'm just looking for a sanity check to see if loading in this manner is the best way?

Also, is there a way to load a chunk of the target text file, parse the chunk, request another chunk, etc. Kind of like a music player buffering a song while playing the song.

Thank you

解决方案

is there a way to load a chunk of the target text file, parse the chunk, request another chunk, etc.

To retrieve a portion (or chunk) of a resource, use the HTTP Range header. Most web servers honor this header correctly.

example HTTP Request:

GET /resource/url/path HTTP/1.1\r\n
User-agent: whatever\r\n
Host: www.example.com\r\n
Range: bytes=0-3200\r\n\r\n

Update:

With jQuery, v1.5 and later, you can tell jQuery to send additional HTTP headers for an individual ajax call, like this:

    $.ajax({type: "GET",
            url: url,
            headers : { "Range" : 'bytes=0-3200' },
            ....

If you don't want to modify each $.ajax() call, Stofke says you can use a "beforeSend" fn. Something like this:

  $.ajaxSetup({ beforeSend : function(xhr) {
        xhr.setRequestHeader("Range", "bytes=0-3200" );
  }});      

Using $.ajaxSetup(), you'd need to modify that range header for each additional call.

I don't know of a way to tell jQuery to load a resource chunkwise, automatically. I guess you'd have to do that yourself with setTimeout(), or something, in the success() of the ajax call. Each time you get a response, call setTimeout() and make the next ajax call. Your browser-resident Javascript will need to keep track of which portions of the file you have retrieved already, and which part (byte index) you want to get next.

Another way to do it would be to make the ajax calls only when your app is ready to do so. Rather than waiting for success() from the ajax call, just make an ajax call after you've finished processing the results of the first range retrieval.

Also: to support the arithmetic purposes, before doing the first GET, you can use the HEAD call to learn the length of the resource, which will tell you the max index in the range you can use.

这篇关于使用jQuery加载大文本文件的最佳做法get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆