如何使用Cordova将应用程序包中的文件复制到Documents文件夹?

时间:2015-01-06 09:04:48

标签: ios cordova

如何使用Cordova file API将文件从应用程序包(只读)复制到Documents文件夹(读写)?是否可以不使用an additional plugin

1 个答案:

答案 0 :(得分:0)

是的,它应该是可能的。让我们认为该文件位于www/en_US.json,因此当您引用该文件时,使用的路径应为en_US.json。另一方面,目标是cordova.file.documentsDirectory,由File插件指定。所以现在你知道路径了,你需要做的是用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);
}