首页 > 解决方案 > Ionic 3:直接在`file:///android_asset/www/assets/i18n`中下载和替换文件

问题描述

我正在尝试通过下载并将其存储在应用程序目录中来直接更新翻译文件。下面是我下载和替换文件的代码,

private downloadLanguageFile(url: string, lang: string) {
    const appDir = this.file.applicationDirectory;
    let promise = this.http.get(url, {responseType: 'blob'}).toPromise();
    promise.then((downloadedFile: any) => {
        this.file.writeFile(appDir, `${lang}.json`, downloadedFile, {replace: true})
            .then((data) => {
                console.log(data);
            }, (error) => {
                console.log("error writing file", error);
            })
    }, (error) => {
        console.log("Download language file failed", error);
    });

    return promise;
}

但不是将文件保存在该目录中,而是抛出此错误:

FileError {代码:1,消息:“NOT_FOUND_ERR”} 代码:1 消息:

“NOT_FOUND_ERR”

原型:对象

当我将文件目录更改为 dataDirectory 时,代码运行良好。关于我做错了什么的任何建议?提前致谢。

注意:允许将文件和目录从复制applicationDirectory到任何其他目录,但不允许反向操作。

标签: cordovaionic3angular5

解决方案


推荐阅读