在浏览器中使用Javascript连续尾随本地文件以换行 [英] Continiously tail a local file for new lines using Javascript in a browser

查看:95
本文介绍了在浏览器中使用Javascript连续尾随本地文件以换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,该文件经常添加新行.我想创建一个使用javascript在浏览器中运行的Web应用程序,它将能够检测该本地文件上的更改并打印添加在该文件上的新内容.

I have a file that gets new lines appended to it frequently. I want to create a web application running in the browser using javascript that will be able to detect changes on that local file and print the new content that is added on the file.

我的解决方案包括将JavaScript轮询与setInterval和新的HTML5 File API一起使用.我使用输入文件字段加载文件,然后轮询文件的大小.每次大小更改(以我为例增加),这向我表明已将新内容添加到文件中.然后,我使用Blob对文件进行切片,并且仅将使用旧文件大小作为开始而新文件大小作为结束添加的内容.

My solution includes using javascript polling with setInterval and the new HTML5 File API. I load the file using an input file field and then poll the size of the file. Each time the size changes (in my case increases) this indicates to me that new content has been added to the file. I then slice the file using a Blob and only take the content that was appended using the old file size as start and the new file size as end.

通常以平均间隔(我使用的是500ms)运行良好,但是我很好奇是否有更好的解决方案,因为这对我来说听起来有点像黑客.

Generally it works good with an average interval (I'm using 500ms) but I'm curious if there's a better solution cause this one sounds a bit like a hack to me.

推荐答案

您可以使用 js-logtail 用于以下文件,例如tail -f:

You can use js-logtail for following files like tail -f :

<head>
    <title>habitat parser log viewer</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="logtail.js"></script>
    <link href="logtail.css" rel="stylesheet" type="text/css">
</head>

<body>
    <div id="header">
        js-logtail.
        <a href="./">Reversed</a> or
        <a href="./?noreverse">chronological</a> view.
        <a id="pause" href='#'>Pause</a>.
    </div>
    <pre id="data">Loading...</pre>
</body>

您甚至不需要服务器端部分,网络服务器(例如apache,lighthttpd)将在内部使用HTTP Range标头进行处理.

You don't even need a server side part, the webserver (as apache, lighthttpd) handles this with the HTTP Range header internally.

这篇关于在浏览器中使用Javascript连续尾随本地文件以换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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