javascript HTML5 文件 API:FileReader.readAsText() 返回“未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6415344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:35:27  来源:igfitidea点击:

HTML5 File API: FileReader.readAsText() returns "undefined"

javascripthtmlfilereaderfileapi

提问by Claudio Albertin

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

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

I try to read the contents of a file as text and save it in a data-object with the following function:

我尝试将文件的内容作为文本读取并将其保存在具有以下功能的数据对象中:

this.upload = function(file) {
    console.log('FileHandler.upload called with ' + file.name + '.');
    console.log(file);
    console.log(this.reader);

    data = {
        content: this.reader.readAsText(file)
    }

    console.log('Content: ' + data.content);
}

"file" seams to be a valid file-object and "this.reader" is a fresh instance of type FileReader. This code creates the following console output:

“file”接缝是一个有效的文件对象,而“this.reader”是一个 FileReader 类型的新实例。此代码创建以下控制台输出:

http://cl.ly/1Y2b383G2F272x1m1P0N

http://cl.ly/1Y2b383G2F272x1m1P0N

enter image description here

在此处输入图片说明

采纳答案by pimvdb

That's not the way it works according to the docs. You should call the readAsText()function, and when it's completed the result is stored in .result.

根据文档,这不是它的工作方式。您应该调用该readAsText()函数,并在完成后将结果存储在.result.