Cordova iOS 8 Filereader onload / onloadend无法正常工作

时间:2015-05-22 09:30:23

标签: javascript ios cordova

我正在使用cordova插件file v1.3.2file-transfer v0.5.0来管理从互联网下载文件,然后在应用程序中阅读它。

文件下载和所有正常工作但由于某种原因FileReader已停止工作。几个月前,这一切都运作正常。 onloadstart确实会运行,但onloadonloadend都会运行。

这是我的javascript代码:

var fileURL = "cdvfile://localhost/persistent/wines-1";
var fileTransfer = new FileTransfer();
var uri = encodeURI(APP_CONFIG.tbDevDlUrl+"wine_data_dl.php?file=1");

console.log("[WORKER] Downloading wine file #1 to", fileURL);

fileTransfer.onprogress = function(progressEvent){
    if(progressEvent.lengthComputable){
        var percent = (progressEvent.loaded / progressEvent.total) * 100;
        $("#backgroundNotification").find(".loading-bar").css("width", percent+"%");
    }
};
fileTransfer.download(
    uri,
    fileURL,
    function(entry){
        console.log("[WORKER] Wine file 1 download complete");

        window.resolveLocalFileSystemURL("cdvfile://localhost/persistent/wines-1", gotFile, fail);

        function fail(e){
            console.log("[WORKER] FileSystem Error");
            console.log(e);
            deferred.reject();
        }

        function gotFile(fileEntry){
            console.log("[WORKER] Reached gotFile");
            fileEntry.file(function(file){
                var reader = new FileReader();

                reader.onloadend = function (e) {
                    console.log("[WORKER] Successfully fetched local wines copy file 1 from FileSystem");
                    Wine.tmpData = JSON.parse(this.result);

                    self.updateWineDatabase()
                        .then(function (count) {
                             console.log("[WORKER] First file done. Processed", count, "wines");
                             Wine.tmpData = [];  // garbage management
                        });
                };

                reader.onerror = function(e){
                    console.log("[WORKER] Failed to read file 1");
                    console.log(e);
                    deferred.reject();
                };

                reader.error = function(e){
                    console.log("[WORKER] Failed to read file 1");
                    console.log(e);
                    deferred.reject();
                };

                reader.readAsText(file);
            })
        }
    },
    function(error){
        console.log("[WORKER] Failed to download wine file 1. source", error.source, "target", error.target, "code", error.code);
        deferred.reject();
    },
    false
);

我还尝试了应用文档文件夹cordova.file.documentsDirectory,但它也没有在那里工作。

1 个答案:

答案 0 :(得分:0)

我终于设法解决了这个问题。这是一个无效的JSON文件。因此,将来有人在使用FileReader读取json文件时遇到问题,请确保您的JSON有效。