【问题标题】:Where are files stored with cordova-file-plugin使用 cordova-file-plugin 存储的文件在哪里
【发布时间】:2019-12-20 09:32:18
【问题描述】:

我正在尝试在移动设备上创建一个 Excel,我正在使用 android 进行测试,但它也应该适用于 iOS

我使用了文档中的以下代码 Documentation

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    console.log('file system open: ' + fs.name);
    fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function (fileEntry) {

        console.log("fileEntry is file?" + fileEntry.isFile.toString());
        fileEntry.name == 'someFile.txt'
        fileEntry.fullPath == '/someFile.txt'
        fileEntry.createWriter(function (fileWriter) {

            fileWriter.onwriteend = function() {
                console.log("Successful file write...");
                fileEntry.file(function (file) {
                    var reader = new FileReader();

                    reader.onloadend = function() {
                        console.log("Successful file read: " + this.result);
                        //displayFileData(fileEntry.fullPath + ": " + this.result);
                    };

                    reader.readAsText(file);

                },);
            };

            fileWriter.onerror = function (e) {
                console.log("Failed file write: " + e.toString());
            };

            let dataObj = new Blob(['some file data'], { type: 'text/plain' });


            fileWriter.write(dataObj);
        });

    });

});

我尝试将前三行更改为以下几行,结果相同

window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (fs) {
    console.log('file system open: ' + fs.name);
    fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function (fileEntry) { ...

我得到以下控制台日志

file system open: persistent    
fileEntry is file?true    
Successful file write...    
Successful file read: some file data

所以,文件已创建并且我可以读取它,但我没有收到任何提示或其他内容,然后我导航到 Android/data/com.myapp.app/files 上的文件,但我没有任何文件

【问题讨论】:

标签: javascript android cordova cordova-plugins android-file


【解决方案1】:

似乎文件正在保存,但我看不到它们,但我可以通过 cordova-file-plugin 读取它们

我将目标文件夹更改为

let ruta = cordova.file.externalRootDirectory
let directoryRoute = "myApp";
            window.resolveLocalFileSystemURL(ruta, function (fs) {
                fs.getDirectory(directoryRoute, { create: true }, function (fs2) {
                    fs2.getFile(fileName, { create: true, exclusive: false }
, function (fileEntry) { ...

如果不存在用于保存我的应用程序文档的文件夹,我正在创建使用 cordova.file.externalRootDirectory,这适用于 android,可能会对 iOS 进行更改

我将在获得 iOS 答案的几天后更新答案,以防这对某人有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2015-11-03
    • 2020-05-06
    • 1970-01-01
    相关资源
    最近更新 更多