【问题标题】:Why isn't getFile() creating any files even though i have set the create flag on true?为什么即使我已将创建标志设置为 true,getFile() 也不创建任何文件?
【发布时间】:2015-12-26 01:46:29
【问题描述】:

我正在尝试将一些数据写入文件并将其保存在移动设备的内存中。我正在开发一个安装了cordova-plugin-file 的cordova 项目。一切似乎都在正常工作,但没有创建任何文件,因此没有保存任何数据...... 为什么即使我将创建标志设置为 true,getFile() 也不创建任何文件?

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);

function success(fileSystem) {
    window.rootFS = fileSystem.root;
    window.rootFS.getFile("TestFile.txt", { create:true }, onSuccess, onFail); 
    function onSuccess() {
        //write something on the file 
    }
    function onFail(error) {
        //show error
    }

function fail(error) {
    alert("Failed to retrieve file: " + error.code);
}

【问题讨论】:

    标签: javascript android file cordova hybrid


    【解决方案1】:

    我找到了一个解决方案:使用 window.resolveLocalFileSystemURL 而不是 window.requestFileSystem。但是我只能使用 cordova.file.externalDataDirectory 访问内部 sd 卡。只是 cordova.file.dataDirectory 给了我一个不存在的路径 file:///data/data/... 以下代码证明适用于 Android。

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dir) {
        console.log("got main dir",dir);
        dir.getFile("log.txt", {create:true}, function(file) {
            console.log("got the file", file);
            logOb = file;
            writeLog("App started");
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      相关资源
      最近更新 更多