Search code examples
angulartypescriptfile-uploadfirebase-storageangularfire2

Uploading a file/image located in Angular Project Directory


I want to upload a file which is stored in my angular assets directory. Below is the code that I use to upload files to firebase.

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

Here the file variable gets assigned with the file which gets selected from the file uploader in html. i need to modify this part/'file' variable to select the file which is located at assets/img/default-avatar.jpg. I tried with toPath and pathToFileURL and also googling. but none of them worked


Solution

  • You can use Angular's 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);
        });
    

    Based on the file type you might want to set proper responseType though.