上传位于Angular Project Directory中的文件/图像

问题描述

我想上传一个文件,该文件存储在我的角度资产目录中。以下是我用于将文件上传到Firebase的代码。

    const file = this.selectedProPic;
    const filePath = `${this.uid}/propic`;
    const fileRef = this.afStorage.ref(filePath);
    this.taskProPic = this.afStorage.upload(filePath,file);

在这里,文件变量被分配了文件,该文件是从html文件上传器中选择的。我需要修改这个part /'file'变量以选择位于asset / img / default-avatar.jpg的文件。我尝试了toPath和pathToFileURL以及谷歌搜索。但他们都不起作用

解决方法

您可以使用Angular的http client

this.http.get('assets/filename.txt',{responseType: 'text'})
    .subscribe(data => { 
         const file = data;
         const filePath = `${this.uid}/propic`;
         this.afStorage.upload(filePath,file);
    });

不过,根据文件类型,您可能需要设置正确的responseType。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...