簡體   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