0

例如,如果我认为 fileReader 必须返回文件的内容值,我只返回空字符串。全局变量对我来说无法使用,只能使用本地。是否可以?
我错了什么?

编辑:

function onDeviceReady() {    
    console.log("==> DEVICE READY");    
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, fileErrorMSG);    
}    

function onFSSuccess(fs) {    
    fileSystem = fs;    
}    

function readlocalFile(fileName) {    
    var core = "";    

    fileSystem.root.getFile(fileName, {create: false}, function(f) {    
        f.file(function(e) {    
            var reader = new FileReader();    
            reader.onloadend = function(evt) {    
                var res =  $.parseJSON(evt.target.result);    
                core = res;    
            };    
            reader.readAsText(e);    
        });//f.file()    
    }, fileErrorMSG);    

    return core;    
}     

function loadDefaultCore(url) {    
    if (url) {    
        var myCore = readlocalFile(url);    
        console.log(myCore); // **output - empty string!!!!!!!!**    
    } else {    
        alert('can not load default core');    
    }    
}    

谢谢!

4

1 回答 1

0

好吧,有很多事情要考虑。简单来说:

  1. 首先,要读取文件,您需要获取文件系统。
  2. 其次,成功后您需要使用文件系统获取文件条目。
  3. 第三,成功时使用文件条目读取文件。

这三个必须通过函数回调链接。

You are getting empty as you file is not being read. I follow the chain an after the onFSSuccess function the chain is broken, the readLocalFile function is not being called, just add it after you assign your file system to the fileSystem variable which I assume is a global variable. Or call your function loadDefaultCore, I am not sure which one you really want to call first.

It will help you if you add more console log messages in each function so you can actually debug the problem easily.

Also, did you have your document even listener attached to the device ready function? Any messages, errors warnings in the console?

From the phonegap file api, follow this and you will be safe. Check the doc for the phonegap version you are working on.

http://docs.phonegap.com/en/2.4.0/cordova_file_file.md.html#FileReader

<script type="text/javascript" charset="utf-8">

// Wait for Cordova to load //function onLoad() { document.addEventListener("deviceready", onDeviceReady, false); }

// Cordova is ready //function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); }

function gotFS(fileSystem) { fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail); }

function gotFileEntry(fileEntry) { fileEntry.file(gotFile, fail); }

function gotFile(file){ readDataUrl(file); readAsText(file); }

function readDataUrl(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("Read as data URL"); console.log(evt.target.result); }; reader.readAsDataURL(file); }

function readAsText(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("Read as text"); console.log(evt.target.result); }; reader.readAsText(file); }

function fail(evt) { console.log(evt.target.error.code); }

</script>
于 2013-09-13T02:27:19.147 回答