【问题标题】:Phonegap cordova.file is undefinedPhonegap cordova.file 未定义
【发布时间】: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


    【解决方案1】:

    一些想法:

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

    【讨论】:

      【解决方案2】:

      我刚遇到这个问题,在检查 phonegap build 上的构建日志后,我发现我的 cordova-plugin-file 版本需要更新版本的 phonegap,需要设置如下:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-10
        • 1970-01-01
        • 2014-12-01
        • 2014-02-07
        • 2013-07-26
        • 1970-01-01
        • 2013-09-12
        • 2015-09-21
        相关资源
        最近更新 更多