-1

In my React application I correctly show and image (in this case an SVG) and I want that the user can download it clicking on a button. The image is served by Firebase Storage.

It doesn't works because I got he CORS error:

Access to fetch at 'xxxxxxxxxxx from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

enter image description here

This is my code:

export const Download = () => {

  const url = "https://storage.googleapis.com/xxxxxxxxxx-c1d67.appspot.com/KYi5nFkjgJPuIxeDkuzvC8XfP6A3/1673110077807-0.svg?GoogleAccessId=xxxxxxxxxxx"

  const handleClickDownload = (fileUrl: string) => {
    fetch(fileUrl, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/pdf',
      },
    })
      .then((response) => response.blob())
      .then((blob) => {
        // Create blob link to download
        const url = window.URL.createObjectURL(
          new Blob([blob]),
        );
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute(
          'download',
          `filename.svg`,
        );

        // Append to html link element page
        document.body.appendChild(link);

        // Start download
        link.click();

        // Clean up and remove the link
        link.parentNode?.removeChild(link);
      });
  }

  return (
    <div>
      <img src={url} alt='result' />
      <button onClick={() => handleClickDownload(url)}>Download SVG</button>
    </div>
  )
};
fabio
  • 43
  • 7

1 Answers1

-2

As explained here CORS in Firebase Storage must be enabled:

Firebase Storage and Access-Control-Allow-Origin

E_net4
  • 27,810
  • 13
  • 101
  • 139
fabio
  • 43
  • 7