在javascript中读取本地文件 [英] Reading local file in javascript

查看:155
本文介绍了在javascript中读取本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在客户端浏览器中运行的JavaScript中,是否可以通过以下方式访问IO的本地文件?该文件会定期添加(每隔约25毫秒).我们希望读取每条新行的内容.最好在添加任何内容后以异步方式(收到某种信号或执行回调).这可能吗?如果可以,怎么办?

Is there a way to, in JavaScript running in the client's browser, access a local file for IO in following manner? The file is being appended to regularly (every ~25ms). We want to read every new line as it comes in. Preferably asynchronously (we get some kind of signal, or a callback executes) when anything is appended. Is this possible? If so, how?

我对Javascript的经验仅限于D3,因此,对于如何或为什么/为什么不可行的基本解释将不胜感激.我完全可以理解,由于安全原因,这是不可能的.

My experience with Javascript is limited to D3, so a basic explanation on how or why/why not this is feasible would be appreciated. I can totally understand this not being possible due to security reasons.

只是为了清楚起见,该文件在客户端计算机上.该文件只是一个文本文件.附加到该进程的是客户端计算机上的本机进程.

Just to make it clear, the file is on the clients computer. The file is just a text file. The process appending to it is a native process on the client's machine.

推荐答案

答案不是真的,不是.

但是有一些变通办法,具体取决于您可以使用的功能(受支持的浏览器等)

BUT there are some workarounds, depending on what you can get away with (supported browsers, etc)

当您抓取本地文件时(我假设您使用的是<input type="file">,而不是部分受支持的非标准化方法),您会得到一个"change"事件要订阅,但是该更改反映了所选内容中的更改文件,而不是更改特定文件的内容.

When you grab a local file (I'm assuming you're using <input type="file">, rather than partially supported, unstandardized methods), you get a "change" event to subscribe to, but that change reflects the change in selected files, not a change in the contents of a particular file.

除此之外,如果您有Blob或从Blob继承的File,则基本上有一个充满数据的缓冲区.

Moving beyond that, if you have a Blob or a File which inherits from Blob, you basically have a buffer full of data.

该缓冲区不会根据操作系统中其他位置的变化而动态地更新自身,也不支持回调选项.

That buffer isn't going to dynamically update itself, based on changes elsewhere in the OS, nor does it support callback options to do so.

或者,从Blob或文件中,您可以选择使用URL.createObjectURL( file ),它返回一个临时URL,该URL引用文件而不是文件内容.

Alternatively, from a blob or a file, you have the option to use URL.createObjectURL( file ), which returns a temporary url, which references a file, rather than the content of the file.

也就是说,对该文件的任何更改都可能会从对该URI的后续调用中反映出来.
这不是我要测试的东西.

That said, any change to that file might then be reflected from subsequent calls to that URI.
This isn't something I've built to test, yet.

将没有回调;但是,您可以设置基于间隔的心跳,以对该URI进行XHR调用,然后将.responseText与以前的.responseText进行比较.

There would be no callback; however, you could set up an interval-based heartbeat, to make an XHR call to that URI, and then compare the .responseText with the previous .responseText.

再次,不能保证,但是就我所想,您现在就已经可以解决这个问题了(虽然仍然处于插件/无扩展解决方案的领域),并且可能只花了一个小时的时间制作出一个原型,以及更多一些,以测试支持(链接是否处于活动状态,而不是初始表示形式,等等).

Again, no guarantees, but that's as close as I think you're going to get to an answer, right now (while still in the realm of plugin/extension-free solutions), and might just be worth an hour of working out a prototype, and a couple more, testing support (whether the link is live, rather than an initial representation, etc).

var input = document.querySelector("input[type='file']"), // client must open file, themselves
    fileHandle = "", // this will be the url for the local file
    content    = "", // this will be the latest contents of the file
    previousContent = ""; // this will be the previous contents

var ms = 1000,
    secs = 2,

    pollId; // will be the process ID for the polling (so you can stop checking for changes, later).

// wait for a file to be selected
input.addEventListener("change", function (evt) {

    var file = evt.target.files[0]; // grab the file object
    fileHandle = URL.createObjectURL(file); // create a url which points to the local file

    // create an interval to check for changes
    pollId = setInterval(
        getLatest(fileHandle, getCurrent, updateCurrent),
        secs * ms
     );
});

function updateCurrent (latest) {
    current = latest;
    doStuff(current);
    // the text has changed; it's been updated;
    // now call home or do something else
}

function getCurrent () { return current; }

function getLatest (url, getCurrent, onchange) {
    return function () {
        // this isn't actually calling the network
        // it's calling the local file
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url);

        xhr.onload = function () {
            var latest = xhr.responseText;
            if (getCurrent() !== latest) { onchange(latest); }
        };

        xhr.send();
    };
}

您现在可以在支持的浏览器中进行更改轮询.

You now have change polling, in supporting browsers.

这篇关于在javascript中读取本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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