上传位于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。