1

我有一个 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 对象,但代码永远不会达到那样的程度。

我还使用specconfig.xml 文件中的属性尝试了旧版本的插件,但这不起作用。

4

2 回答 2

0

一些想法:

  • 确保正在安装文件插件。您应该能够在 PGB 构建日志中验证这一点。
  • 在网络检查器的网络选项卡中检查 cordova_plugins.js 并验证文件插件是否已列出
  • 检查文件插件 javascript 资源是否也在网络选项卡(File.js 等)中加载
于 2018-02-23T02:54:39.513 回答
0

我刚遇到这个问题,在检查 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

于 2018-09-15T00:48:35.430 回答