简体   繁体   English

通过Ajax / dojo显示大型文本文件

[英]Displaying Large Text Files via Ajax/dojo

I want to display to a user a large text file (100MB Log Files specifically) via a web interface without requiring the user to have to download the entire file. 我想通过Web界面向用户显示一个大文本文件(特别是100MB日志文件),而不需要用户必须下载整个文件。 Obviously returning the entire file to someones web browser would not be sensible, so my theory was to used Ajax to fetch portions of the file depending on the user scrolling through the file, similar to the way Google Maps provides a "window" of the map. 显然将整个文件返回到某人的网页浏览器是不明智的,所以我的理论是使用Ajax取决于用户滚动文件的部分文件,类似于Google Maps提供地图“窗口”的方式。

My application server is PHP, and I fairly sure I can perform the appropriate seeks and reads through the file and return the results via XHR to application, but my Ajax framework is dojo and I can't think of any standard dijit that would work and am trying to figure out how best it would be to impliment something. 我的应用程序服务器是PHP,我相当确定我可以执行适当的搜索并读取文件并通过XHR将结果返回给应用程序,但我的Ajax框架是dojo,我想不出任何可行的标准dijit我试图弄清楚如何最好地捏造某些东西。

Should I derive my own widget? 我应该派生自己的小部件吗? Is there already something out there that I am not aware of? 那里有我不知道的东西吗? If I build my own custom widget, what sort of structure should it take and are there any good resources for developing custom widgets for dojo/dijit? 如果我构建自己的自定义小部件,它应该采用什么样的结构,是否有任何好的资源来开发dojo / dijit的自定义小部件? Any other thoughts? 还有其他想法吗?

This seems to be a tut on what you might need I would suggest that you use an li, because you will end up wanting to perform some actions on each line, most likely each line will be relevant. 这似乎是对你可能需要的东西的啧啧我建议你使用li,因为你最终想要在每一行上执行一些操作,很可能每条线都是相关的。

Scrolling is nice, but you can also just blit the interface with pagination, meaning they click next page, previous page, and you fetch it, then update the view. 滚动很不错,但您也可以通过分页来显示界面,这意味着他们单击下一页,上一页,然后获取它,然后更新视图。 That's the easiest method. 这是最简单的方法。 With scrolling, you'll need to get more above and below the current visible lines for seamless scrolling. 通过滚动,您需要在当前可见线的上方和下方获得更多,以实现无缝滚动。

For instance, if you want to show 25 lines, you'll need to fetch 25 + bottom pad on the first go, and define the lines showing in bottom pad as the threshold for signalling a new event to download an extra 25+ bottom pad items. 例如,如果要显示25行,则需要在第一次使用时获取25 +底部打击垫,并将底部打击垫中显示的行定义为用于指示新事件下载额外25+底部打击垫的阈值项目。

With a 100mb file, that's gonna get sluggish soon, so you'll have to clear out the previous entries, and define a new top pad to signal a request to get the reverse. 有了一个100mb的文件,很快就会变得缓慢,所以你必须清除之前的条目,并定义一个新的顶部填充来表示反向请求。 That is to say, 1st req: fetch 25 + bottom pad, 2nd req fetch 25 + bottom pad remove prev 25 - top pad. 也就是说,第一次要求:获取25 +底部垫,第二次获取25 +底部垫移除前25 - 顶部垫。

One thing to note is, when you do this, in firefox at least, it can tend to get wonky and not fire events after a few loads, so you may want to unbind/rebind your even listeners. 有一点需要注意的是,当你这样做时,至少在firefox中,它可能会在几次加载后变得不稳定并且不会触发事件,所以你可能想要取消绑定/重新绑定你的偶数听众。 I only say this because I have a friend who is currently working on something with similar functionality, and these are some of the issues he came across. 我之所以这么说,是因为我有一位朋友正在研究具有类似功能的东西,这些是他遇到的一些问题。

No one is going to complain that they have to click next page/previous page, it'll be fast and clean, but mess up your scrolling and no one will want to use your widget. 没有人会抱怨他们必须点击下一页/上一页,它会快速而干净,但是搞乱你的滚动,没有人会想要使用你的小部件。

Here are some other resources on the topic: Old Ajax Scrollable Table - Twitter like load more tut - Good scrolling example, read the source - Check out this googlecode project 以下是关于该主题的一些其他资源: 旧Ajax可滚动表 - Twitter喜欢加载更多tut - 良好的滚动示例,阅读源 - 查看此googlecode项目

I recommend caching. 我建议缓存。

It should be noted that the solution to this problem should take into account that reading a sufficiently large file (100mb+) from disk is going to be disk bound and likely to outrun any timeout that your web server has set for script execution time. 应该注意的是,这个问题的解决方案应该考虑到从磁盘读取一个足够大的文件(100mb +)将是磁盘绑定的,并且可能超过您的Web服务器为脚本执行时间设置的任何超时。 In order to avoid making the user wait an inordinate amount of time to load any portion of the file I would avoid hacks like changing your server's timeout limits. 为了避免让用户等待过多的时间来加载文件的任何部分,我会避免像更改服务器的超时限制这样的黑客攻击。

Here's one possible solution that comes to mind: 1) Cache the file by chopping it up into separate files. 这是一个可能的解决方案:1)通过将文件切割成单独的文件来缓存文件。 You can easily do this in a cron job or even trigger it when the file is written. 您可以在cron作业中轻松完成此操作,甚至可以在写入文件时触发它。 Use readfile_chunked ( http://cn2.php.net/manual/en/function.readfile.php#48683 ) or similar. 使用readfile_chunked( http://cn2.php.net/manual/en/function.readfile.php#48683 )或类似内容。

2) Write a service handler script that when invoked from the browser (say './readfile?chunk=##') returns the requested chunk. 2)编写一个服务处理程序脚本,当从浏览器调用时(例如'./readfile?chunk=##')返回请求的块。

3) Use a pagination widgit or a scroller as suggested by the other contributor to make the call to the service handler via AJAX. 3)使用其他贡献者建议的分页widgit或滚动条通过AJAX调用服务处理程序。

Cons: This will inevitably increase the amount of disk space. 缺点:这将不可避免地增加磁盘空间量。 Pros: Happy users as disk access will be optimized and so will script execution time. 优点:作为磁盘访问的快乐用户将被优化,脚本执行时间也将优化。 Also, it scales well. 而且,它可以很好地扩展。 (on the order of O(n)). (按O(n)的顺序)。

Have you considered using Dojo Grid for viewing logs? 您是否考虑过使用Dojo Grid查看日志? It has built-in support for dynamic loading of 'pages' ie rows of data. 它内置支持动态加载“页面”即数据行。

If the log file is a text file with a consistent line ending, maybe you can fetch it by line number. 如果日志文件是具有一致行结尾的文本文件,则可以按行号获取它。

I have idea with the algorithm like this: 我对这个算法有这样的想法:

  1. When page loaded, fetch first 100 line from file. 加载页面时,从文件中获取前100行。 put it in some container, maybe a div, textarea, or using <ul><li> 把它放在一些容器中,可能是div,textarea,或者使用<ul><li>
  2. Put an event handler to know that user have scrolling to the last part of container. 放置事件处理程序以了解用户已滚动到容器的最后部分。
  3. Send AJAX request to get next 100 lines from the file. 发送AJAX请求以从文件中获取下一行100行。 Pass ithe line offset as parameter (GET or URI Parameter) so the PHP script can get the right part of the file 将行偏移作为参数(GET或URI参数)传递,以便PHP脚本可以获取文件的正确部分
  4. Put the AJAX response to the end of container, update next AJAX request offset. 将AJAX响应放到容器的末尾,更新下一个AJAX请求偏移量。
  5. If no more lines in file left, return an empty response. 如果文件中没有更多行,则返回空响应。 AJAX handler should consider this as end of file so will remove event handler in step 2 above. AJAX处理程序应将此视为文件的结尾,因此将在上面的步骤2中删除事件处理程序。

I don't know much about Dojo. 我对Dojo了解不多。 I use jquery tools's scrollable in my application. 我在我的应用程序中使用jquery工具的可滚动 It's easy to put an event handler when the scroller reach last page, then fetch next item. 当滚动器到达最后一页时,很容易放置一个事件处理程序,然后获取下一个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM