繁体   English   中英

如何使用Cordova将文件从应用程序捆绑包复制到Documents文件夹?

[英]How to copy a file from app bundle to the Documents folder with Cordova?

如何使用Cordova文件API将文件从应用程序捆绑包(只读)复制到Documents文件夹(读写)? 是否可以不使用其他插件

是的,应该有可能。 让我们认为该文件位于www/en_US.json因此当您引用它时,使用的路径应该是en_US.json 另一方面,目标是由File插件指定的cordova.file.documentsDirectory 因此,现在您知道路径了,您需要做的是使用File插件复制文件。 这是完全未经测试的示例,但应为您提供主要思想:

function copyFileToDocuments(fileToBeCopied) {
    function gotFileEntry(fileEntry) {
        fileEntry.copyTo(cordova.file.documentsDirectory);
    }
    function onFileSystemSuccess(fileSystem) {
        window.resolveLocalFileSystemURL(this.getDirectory(), function(dirEntry) {
            dirEntry.getFile(fileToBeCopied, {create: true, exclusive: false}, gotFileEntry.bind(this),     this.errorHandler);
        }.bind(this));
    }

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess.bind(this), this.errorHandler);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM