文件阅读失败

时间:2014-10-24 22:03:31

标签: javascript file cordova

此刻脚本正在Android OS上运行。

全球文件

///Store Global Variables
var Globals = {
    Storage: window.localStorage,
    OfflineMode: false,
    GetSettingsString : function()
    {
        return JSON.stringify(Settings);
    },

    SetSettings : function(str)
    {
        try
        {
            Settings = JSON.parse(str);

            if(Settings.Id != 0)
                VelocityMeetings.app.navigate("scan/", { root: true });
        }
        catch(e)
        {
            alert(e);
            Globals.SetSettings();
        }
    },
    ///Experimentation Function
    SetSettings: function () {
        //Settings = JSON.parse(str);

        Settings.OfflineMode = false,
            Settings.Username = "mager1794",
            Settings.Password = "mn1apwfm",
            Settings.Id = 0;

        alert("Values Set Manually");

        //VelocityMeetings.app.navigate("scan/", { root: true });
    },

    Init: function () {
        // this.SetSettings(/*FileStream.ReadFile("settings.dat")*/);
        alert("test2");
        this.SetSettings(FileStream.ReadFile("settings.dat"));
        alert("test3");

    },

    Save: function () {
        FileStream.WriteFile("settings.dat", GetSettingsString());
    }

 };

document.addEventListener("deviceready", ondeviceReady(), false);



 // Cordova is ready to be used!
//
function ondeviceReady() {
    alert("test");
    Globals.Init();

}

文件系统文件

var FileStream = {

    WriteFile: function (filename, objtoWrite) {

        _filename = filename;
        _dataobj = objtoWrite;
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, _gotFSWrite, fail);
    },

    WriteFile: function (filename, objtoWrite, type) {

        _filename = filename;
        _dataobj = objtoWrite;
        _type = type;
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, _gotFSWrite, fail);
    },

    ReadFile: function (filename) {
        alert("ReadFile Called");
        _filename = filename;
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, _gotFSRead, fail);

        return _dataread;
    },

    _dataread: null,
    _dataobj: null,
    _type : "WRITE",
    _filename: "",
    _gotFileWriter: function (writer) {
        writer.onwrite = function (evt) {
            _isBusy = false;
        };
        if(_type=="WRITE")
            writer.write(_dataobj);
        if (_type == "APPEND")
        {
            writer.seek(writer.length);
            writer.write(_dataobj);
        }
        writer.abort();
    },

    _gotFSWrite: function (fileSystem) {
        fileSystem.root.getFile(_filename, { create: true }, _gotFileEntryWrite, fail);
    },
    _gotFileEntryWrite: function (fileEntry) {
        fileEntry.createWriter(_gotFileWriter, fail);
    },
    _gotFSRead: function (fileSystem) {
        alert("gotFSRead Called");
        fileSystem.root.getFile(_filename, { create: true }, _gotFileEntryRead, fail);
    },
    _gotFileEntryRead: function (fileEntry) {
        alert("gotFileEntryRead Called");
        fileEntry.file(_gotFileRead, fail);
    },
    _gotFileRead: function (file) {
        alert("gotFileRead Called");
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            _dataread = evt.target.result;
        };
        reader.readAsText(file);

    },
    _fail: function (error) {
        throw "File Failed";

    }
};

GotFSRead函数永远不会被触及,我无法弄清楚为什么,我已经放入警报中,因此我可以看到它在功能中的进展。另外,你可以在变量中存储回调吗?因为看起来读文件函数需要回调才能成功接收数据。

1 个答案:

答案 0 :(得分:0)

是的,在Javascript函数中是对象,因此可以将它们分配给变量,就像您在此处的代码中所做的一样。

FileStream对象中的函数引用缺少this引用,例如this._gotFSRead。例如:

ReadFile: function (filename) {
    alert("ReadFile Called");
    this._filename = filename;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, this._gotFSRead, this._fail);

    return this._dataread;
},

有很多地方缺少此功能,您也需要修复_fail个引用。