Phonegap cordova.file未定义

时间:2018-02-21 17:06:48

标签: cordova cordova-plugins phonegap cordova-plugin-file

我有一个Phonegap应用,我试图将文件保存到设备的存储空间,但是我在config.xml文件中包含插件cordova-plugin-file之后,cordova.file对象仍未定义。我正在使用clg cli-6.5.0使用Phonegap Build构建应用程序。

使用Chrome检查器工具,我可以看到,当我尝试记录cordova.file对象时,它返回undefined,logging cordova会返回日志中的cordova对象。

以下是来自控制台的日志,如上所述:

Console testing

以下行位于我的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属性尝试了旧版本的插件,但这不起作用。

2 个答案:

答案 0 :(得分:0)

一些想法:

  • 确保已安装文件插件。您应该能够在PGB构建日志中验证这一点。
  • 检查网络检查器的“网络”标签中的cordova_plugins.js并验证是否列出了文件插件
  • 检查文件插件javascript资源是否也在网络选项卡(File.js等)中加载

答案 1 :(得分:0)

我只是遇到了这个问题,在检查了phonegap的构建日志后,我发现我的cordova-plugin-file版本需要更新的phonegap版本,需要将其设置为:

<preference name="phonegap-version" value="cli-8.0.0" />

http://docs.phonegap.com/phonegap-build/configuring/preferences/#phonegap-version