我有一个 Phonegap 应用程序,我在其中尝试将文件保存到设备的存储中,但是在将插件包含cordova-plugin-file
在 config.xml 文件中之后,该cordova.file
对象仍未定义。我正在使用 cli 使用 Phonegap Build 构建应用程序cli-6.5.0
。
使用 Chrome 检查器工具,我可以看到当我尝试记录cordova.file
对象时,它返回未定义,记录cordova
返回日志中的 cordova 对象。
以下是来自控制台的日志,如上所述:
以下行在我的 config.xml 文件中:
<plugin name="cordova-plugin-file" source="npm" />
<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
这是我用来保存文件的代码:
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, function(dir) {
console.log("Creating file");
var fileName;
do {
fileName = prompt("Please enter a name for your file", "");
} while(fileName == null || fileName == "");
dir.getFile(fileName, {create:true}, function(file) {
console.log("File created succesfully");
file.createWriter(function(fileWriter) {
console.log("Writing content to file");
fileWriter.write(imageBlob);
}, function(){
console.log('Unable to save file');
});
});
});
但是,此功能永远不会运行,并且我没有通过 Chrome 检查器工具在控制台中看到任何日志。imageBlob
是一个包含 base64 图像的 Blob 对象,该图像已转换为 Blob 对象,但代码永远不会达到那样的程度。
我还使用spec
config.xml 文件中的属性尝试了旧版本的插件,但这不起作用。