繁体   English   中英

Phonegap(3.3.0)FileReader readAsText()无法正常工作

[英]Phonegap (3.3.0) FileReader readAsText() not working

因此,我尝试使用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.')});

在您的示例中,_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);
        });
    }
);

暂无
暂无

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

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