【发布时间】:2018-02-21 17:06:47
【问题描述】:
我有一个 Phonegap 应用程序,我在其中尝试将文件保存到设备的存储中,但在 config.xml 文件中包含插件 cordova-plugin-file 后,cordova.file 对象仍未定义。我正在使用 cli cli-6.5.0 使用 Phonegap Build 构建应用程序。
使用 Chrome 检查器工具,我可以看到,当我尝试记录 cordova.file 对象时,它返回 undefined,记录 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 是一个 Blob 对象,其中包含一个转换为 Blob 对象的 base64 图像,但代码永远不会达到那样的程度。
我也尝试过使用 config.xml 文件中的 spec 属性的旧版本插件,但这不起作用。
【问题讨论】:
标签: cordova cordova-plugins phonegap cordova-plugin-file