I uploaded some pdf files in firebase storage and trying access the pdf url and giving user to downlaod the respective file on click of the download file button , but its giving me CORS policy error onclick of download file button.
this is code BookDetails.js
import React, { useEffect, useState } from "react";
import styles from "./Bookdetails.module.scss";
import { Link, useParams } from "react-router-dom";
import { db } from "./../../../Firebase/config";
import { doc } from "firebase/firestore";
import { getDoc } from "firebase/firestore";
import { toast } from "react-toastify";
import spinnerImg from "../../../Assets/spinner.jpg";
const BookDetails = () => {
const { id } = useParams();
const [book, setBook] = useState(null);
const getBook = async () => {
const docRef = doc(db, mycollectionName, id);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
// console.log("Document data:", docSnap.data());
const obj = {
id: id,
...docSnap.data(),
};
setBook(obj);
} else {
toast.error("BOOK NOT FOUND ");
}
};
const downloadFileUrl = (url) => {
fetch(url)
.then((response) => response.blob())
.then((blob) => {
const blobUrl = window.URL.createObjectURL(new Blob([blob]));
const parts = url.split("/");
const fileName = parts[parts.length - 1].split("?")[0];
const cleanedFileName = fileName
.replace(/^[\d]+/, "")
.replace(/%20/g, " ")
.trim();
const aTag = document.createElement("a");
aTag.href = blobUrl;
aTag.setAttribute("Download", cleanedFileName);
aTag.click();
aTag.remove();
});
};
useEffect(() => {
getBook();
}, []);
return (
<section>
<div className={`container ${styles.book}`}>
<h2>Book Details</h2>
<div>
<Link to="#/books">⬅ Back to Homepage</Link>
</div>
{book === null ? (
<img src={spinnerImg} alt="Loading....." />
) : (
<>
<div className={styles.details}>
<div className={styles.img}>
<img src={book.imageUrl} alt={book.name} />
</div>
<div className={styles.content}>
<h3>{book.name}</h3>
<p className={styles.author}>Author : {book.author}</p>
<p>{book.desc}</p>
<p>
Category : <b>{book.category}</b>
</p>
<p>
Language : <b>{book.language}</b>
</p>
<p>
Publisher : <b>{book.publisher}</b>
</p>
{book.file && (
<button
onClick={() => {
downloadFileUrl(book.file);
}}
>
Download File
</button>
)}
</div>
</div>
</>
)}
</div>
</section>
);
};
export default BookDetails;
This is the image of error enter image description here
I want to solve this error but can't able to solve it . Can anyone tell me how to solve CORS policy error from firebase.
Thank you !!