首页 > 解决方案 > 使用 cordova-file-plugin 存储的文件在哪里

问题描述

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

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

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 上的文件并且我没有任何文件

标签: javascriptandroidcordovacordova-pluginsandroid-file

解决方案


似乎文件正在保存,但我看不到它们,但我可以通过 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 的答案时,这可以帮助某人


推荐阅读