如何在科尔多瓦项目中保存文件?

How to save file in cordova project?

我正在尝试使用 cordova-plugin-file 保存文件。 该脚本似乎有效,但我无法在设备上找到该文件,而且 cdv: 引用似乎无法解析。

export const SaveFile = function(fFile, sFileName) {
    return new Promise((resolve, reject) => {
        console.log("requestFilesystem", fFile);
        window.requestFileSystem(
            LocalFileSystem.PERSISTENT,
            fFile.size,
            function(fs) {
                console.log("got fs", fs);
                fs.root.getFile(
                    sFileName,
                    { create: true, exclusive: false },
                    function(fileEntry) {
                        console.log("got fileentry", fileEntry);
                        fileEntry.createWriter(function(fileWriter) {
                            fileWriter.onwriteend = (eEvent) => {
                                console.log(
                                    `${ fileWriter.localURL } saved`,
                                    `${ fFile.size }kb`
                                );
                                resolve(fileWriter);
                            };

                            fileWriter.onerror = (eError) => {
                                console.error('onErrorCreateWriter', JSON.stringify(eError));
                                reject(eError);
                            };

                            fileWriter.write(fFile);
                        });
                    },
                    (eError) => {
                        console.error('onErrorCreateFile', eError);
                        reject(eError);
                    }
                );
            },
            (eError) => {
                console.error('onErrorLoadFs', eError.message);
                reject(eErroreError.message);
            }
        );
    });
};

我在日志中得到以下信息:

SaveFile.js:3 requestFilesystem Blob {size: 1707639, type: "video/webm"}
SaveFile.js:8 got fs FileSystem {name: "persistent", root: DirectoryEntry}
SaveFile.js:13 got fileentry FileEntry {isFile: true, isDirectory: false, name: "1eb4d914-3d81-a313-7744-bea86b90f042.mp4", fullPath: "/1eb4d914-3d81-a313-7744-bea86b90f042.mp4", filesystem: FileSystem, …}
SaveFile.js:16 cdvfile://localhost/persistent/1eb4d914-3d81-a313-7744-bea86b90f042.mp4 saved 1707639kb

所以它保存对了吗?但我没有在 fs 和 cdvfile 上看到视频:视频标签中的 ref 出现损坏。

试图在组件中使用文件的视频标签只是:

<video controls="" src="cdvfile://localhost/persistent/1eb4d914-3d81-a313-7744-bea86b90f042.mp4"></video>

最后我发现了其他似乎有效的步骤。 在 phone.

上找到 fies 之前,我需要请求一个文件夹(它必须是缓存文件夹)
export const SaveFile = function(fFile, sFileName) {
    return new Promise((resolve, reject) => {
        window.resolveLocalFileSystemURL(
            cordova.file.externalCacheDirectory,
            function(directoryEntry) {
                directoryEntry.getFile(
                    sFileName,
                    { create: true },
                    function(fileEntry) {
                        fileEntry.createWriter(
                            function(fileWriter) {
                                fileWriter.onwriteend = (eEvent) => {
                                    resolve(fileWriter.localURL);
                                };

                                fileWriter.onerror = (eError) => {
                                    console.error('onErrorCreateWriter', JSON.stringify(eError));
                                    reject(eError);
                                };

                                fileWriter.write(fFile);
                            },
                            (eError) => {
                                console.error('onErrorCreateFile', eError);
                                reject(eError);
                            }
                        );
                    }
                );
            }
        );
    });
};