Home > Mobile >  Why my download button is not doing anything? Firebase, react
Why my download button is not doing anything? Firebase, react

Time:11-27

I'm following the firebase documentation for web to download the files related to a document in firestore. I practically pasted the code to achieve this, but when I click the element is not showing anything on console.

import { ref, getDownloadURL } from 'firebase/storage'

export const downloadMethod = (path) => {
    getDownloadURL(ref(storage, path))
        .then(url => {
            const xhr = new XMLHttpRequest();
            xhr.responseType = 'blob';
            xhr.onload = (event) => {
                const blob = xhr.response;
            };
            xhr.open('GET', url);
            xhr.send();
        })
        .catch(error => {
            throw error
        })
}

Before this I was having cors error but I solved it using

[
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]

I want the website to download the requested file when I hit the button.

CodePudding user response:

I guess you are missing the reference to the storage

import { getStorage, ref, getDownloadURL } from "firebase/storage";
const storage = getStorage();
...

CodePudding user response:

Is prompting the file download the problem? If so, see, see this answer.

  • Related