Phonegap(3.3.0)FileReader readAsText()无效

时间:2014-06-03 13:54:10

标签: android ios mobile cordova cordova-plugins

因此我尝试使用phonegap文件AP​​I保存并稍后在应用中加载文件。保存似乎正在工作,但读取文件会引发此错误:

processMessage failed: Stack: TypeError: Object #<an Object> has no method 'readAsText'
  at [object Object].readAsText (file:///android_asset/www/plugins/org.apache.cordova.file/www/FileReader.js:130:33)
  at file:///android_asset/www/index.html:3843:15
  at file:///android_asset/www/plugins/org.apache.cordova.file/www/DirectoryEntry.js:100:9
  at Object.callbackFromNative (file:///android_asset/www/phonegap.js:292:54)
  at processMessage (file:///android_asset/www/phonegap.js:1029:21)
  at Function.processMessages (file:///android_asset/www/phonegap.js:1063:13)
  at pollOnce (file:///android_asset/www/phonegap.js:933:17)
  at pollOnceFromOnlineEvent (file:///android_asset/www/phonegap.js:928:5)

无论我做什么,似乎总是抛出这个错误。我将fileReader对象打印到控制台并使用weinre进行检查。它上面有readAsText()函数的原型,所以我真的不知道它为什么不工作......

这就是我保存文件的方式:

var request = window.requestFileSystem;
if(typeof request != 'undefined') {
    var fileSystem;
    var writer;
    request(LocalFileSystem.PERSISTENT, 0, function (FS) {
        fileSystem = FS;
        fileSystem.root.getFile("offlineData.txt", {create: true, exclusive: false}, function(fileEntry) {
            fileEntry.createWriter(function(w) {
                writer = w;
                writer.write('This is some text yo');
            }, function(e) {console.log(e);});}, 
        function(e) {console.log(e); console.log('There was an error getting the file to write')});} , 
    function(e) {\console.log('There was an error getting the file system');});}    

稍后在流程中,我会做这样的事情:

request(LocalFileSystem.PERSISTENT, 0, function(FS) {
                    fileSystem = FS;
                    fileSystem.root.getFile("offlineData.txt", null, function(_file) {
                        var reader = new FileReader();
                        reader.onloadend = function (evt) {
                            console.log("read success");
                            console.log(evt.target.result);
                        };
                        reader.onerror = function(evt) {
                            console.log("Error read text");
                            console.log("Error"+evt.error.code);                                
                        };
                        reader.onabort = function(evt) {
                            console.log("aborted read text");
                            console.log(evt.target.result);                             
                        };
                        reader.onloadstart = function(evt) {
                            console.log("started reading");
                        };
                        console.log(reader);
                        reader.readAsText(_file);                               
                    });
                }, function(e) {console.log(e); console.log('There was an error getting the file.')});

1 个答案:

答案 0 :(得分:1)

在您的示例中,_file是fileEntry而不是文件内容。你能试试这个:

fileSystem.root.getFile("offlineData.txt", null,
    function (fileEntry) {
        fileEntry.file(function (_file) {
            var reader = new FileReader();
            reader.onloadend = function () {
                console.log("read success");
                console.log(evt.target.result);
            };
            reader.readAsText(_file);
        });
    }
);