我正在尝试使用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>
答案 0 :(得分:0)
最后,我发现了其他可行的步骤。 我需要先请求一个文件夹(必须是缓存文件夹),然后才能在手机上找到小伙伴。
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);
}
);
}
);
}
);
});
};