HTML5 文件 API:发生错误后 FileReader 对象未定义

发布于 2024-11-16 11:16:57 字数 1144 浏览 2 评论 0原文

我在 Mac OS X 上使用 Chrome 12,并且在文档中包含了 jQuery 1.6.1。

我尝试使用以下代码读取文件,读取文件时发生错误,因此调用 this.error.onerror() ,但 FileReader-Object this.reader 不再存在,我不能得到错误。我也不确定为什么会发生错误,这是我想阅读的常规文本文档。

function FileHandler(files, action) {
    console.log('FileHandler called.');

    this.files = files;
    this.reader = new FileReader();
    this.action = action;

    this.handle = function() {
        console.log('FileHandler.handle called.');

        for (var i = 0; i < this.files.length; i++) {
            this.reader.readAsDataURL(files[i]);
        }
    }

    this.upload = function() {
        console.log('FileHandler.upload called.');
        console.log(this.reader);

        data = {
            content: this.reader.result
        }

        console.log(data);
    }

    this.error = function() {
        console.log('An error occurred while reading a file.');
        console.log(this.reader.error);
    }

    this.reader.onload = this.upload;
    this.reader.onerror = this.error;
}

此代码创建以下控制台输出: http://cl.ly/0j1m3j0o3s071Y1K3A25

I use Chrome 12 on Mac OS X and I've included jQuery 1.6.1 within the document.

I try to read a File with the following code, an error seams to occur while reading the file, so this.error.onerror() is called, but the FileReader-Object this.reader doesn't exists anymore and I can't get the error. I'm also not sure why the error occurs, it's a regular text document I want to read.

function FileHandler(files, action) {
    console.log('FileHandler called.');

    this.files = files;
    this.reader = new FileReader();
    this.action = action;

    this.handle = function() {
        console.log('FileHandler.handle called.');

        for (var i = 0; i < this.files.length; i++) {
            this.reader.readAsDataURL(files[i]);
        }
    }

    this.upload = function() {
        console.log('FileHandler.upload called.');
        console.log(this.reader);

        data = {
            content: this.reader.result
        }

        console.log(data);
    }

    this.error = function() {
        console.log('An error occurred while reading a file.');
        console.log(this.reader.error);
    }

    this.reader.onload = this.upload;
    this.reader.onerror = this.error;
}

This code creates the following console output:
http://cl.ly/0j1m3j0o3s071Y1K3A25

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-11-23 11:16:57

.onerror 内部,this 与外部不同,因为它是一个新函数(具有新作用域)。

通过如下静态设置来跟踪 this

var _this = this; // _this won't change
this.reader.onerror = function() {
    console.log('An error occurred while reading a file.');
    console.log(_this.reader.error);
}

Inside .onerror, the this is not the same as outside because it's a new function (with a new scope).

Keep track of the this by setting it statically like this:

var _this = this; // _this won't change
this.reader.onerror = function() {
    console.log('An error occurred while reading a file.');
    console.log(_this.reader.error);
}
一个人的夜不怕黑 2024-11-23 11:16:57

这应该是正确的方法:

reader.onerror = function(event) {
    console.error("File could not be read: " + event.target.error);
};

This should be the correct way to do it:

reader.onerror = function(event) {
    console.error("File could not be read: " + event.target.error);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文