简体   繁体   English

Phonegap FileReader readAsText返回null,但readAsDataURL起作用

[英]Phonegap FileReader readAsText returns null but readAsDataURL works

I'm using Phonegap to download an archive, unzip it, then read the files. 我正在使用Phonegap下载档案,将其解压缩,然后阅读文件。 It's all working until I try and read the files as text. 一切正常,直到我尝试以文本形式读取文件。 If I use readAsDataURL() then I get a whole heap of stuff logged to the console. 如果我使用readAsDataURL()那么我会得到一堆东西记录到控制台。

function( file ) {
    console.log(file);
    var reader = new FileReader();
    reader.onloadend = function( evt ) {
        console.log( evt.target.result );
    };                      
    reader.readAsDataURL( file );
}

If I use readAsText() I get null . 如果我使用readAsText()得到null The files range from 300KB to 1.4MB, but all files return null in the console. 文件范围从300KB到1.4MB,但是所有文件在控制台中都返回null

reader.readAsText( file );          

Why would one function return something and the other be null? 为什么一个函数返回某些内容而另一个为null? Is there a limit on the text size it can read? 可以读取的文字大小有限制吗?

This is the file object that I'm logging before creating reader , that I'm applying the functions to (I've shortened the file name): 这是我在创建reader之前记录的file对象,我正在将函数应用到其中(我缩短了文件名):

{
    "name":"categories.json",
    "fullPath":"/var/mobile/.../Documents/data/file.json",
    "type":null,
    "lastModifiedDate":1380535318000,
    "size":382456
}

And this is the evt object for readAsText() : 这是readAsText()的evt对象:

{
    "type":"loadend",
    "bubbles":false,
    "cancelBubble":false,
    "cancelable":false,
    "lengthComputable":false,
    "loaded":0,
    "total":0,
    "target":{
        "fileName":"/var/mobile/.../Documents/data/file.json",
        "readyState":2,
        "result":"null",
        "error":null,
        "onloadstart":null,
        "onprogress":null,
        "onload":null,
        "onerror":null,
        "onabort":null
    }
}

UPDATE : I've seen in the W3C spec for the File API that result would only be set to null if an error had occured. 更新 :我在W3C规范中看到了File API,如果发生错误,结果只会设置为null。 But I tried adding a reader.onerror() function, but that wasn't getting called. 但是我尝试添加一个reader.onerror()函数,但是没有得到调用。

If an error occurs during reading the blob parameter, set readyState to DONE and set result to null. 如果在读取blob参数期间发生错误,请将readyState设置为DONE并将结果设置为null。 Proceed to the error steps. 继续执行错误步骤。

http://www.w3.org/TR/FileAPI/#dfn-readAsText http://www.w3.org/TR/FileAPI/#dfn-readAsText

You may have been grabbing the fileEntry instead of a fileObject. 您可能正在获取fileEntry而不是fileObject。 Assuming file was actually fileEntry, try this: 假设文件实际上是fileEntry,请尝试以下操作:

var
    fileEntry = file, //for example clarity - assumes file from OP's file param
    reader = new FileReader()
;

fileEntry.file( doSomethingWithFileObject );//gets the fileObject passed to it

function doSomethingWithFileObject(fileObject){

    reader.onloadend = function(e){
        doSomething(e.target.result); //assumes doSomething defined elsewhere
    }

    var fileAsText = reader.readAsText(fileObject);
}

Definitely an API that screams for cruft reduction. 绝对是可以减少杂物的API。

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

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