【问题标题】:Uploading a file/image located in Angular Project Directory上传位于 Angular 项目目录中的文件/图像
【发布时间】:2020-12-17 00:08:55
【问题描述】:

我想上传一个文件,该文件存储在我的 angular assets 目录中。下面是我用来将文件上传到 firebase 的代码。

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

在这里,文件变量被分配给从 html 文件上传器中选择的文件。我需要修改此部分/“文件”变量以选择位于 assets/img/default-avatar.jpg 的文件。我尝试使用 toPath 和 pathToFileURL 以及谷歌搜索。但他们都没有工作

【问题讨论】:

    标签: angular typescript file-upload firebase-storage angularfire2


    【解决方案1】:

    你可以使用 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。

    【讨论】:

    • 成功了!谢谢!我想上传一张图片。所以我把 responseType 改成了 'blob'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2018-05-27
    • 2019-07-31
    • 2014-09-28
    • 2011-05-19
    • 2015-08-24
    相关资源
    最近更新 更多