How to save a PDF as a BLOB in PostgreSQL using Sequelize and download it in React and NodeJS with file-saver?

288 views Asked by At

I have the DropZone page where I drag and drop a pdf which is saved in a postgre db with sequelize. I have a compenent where I want to transform that pdf saved as a BLOB back to pdf and then download that pdf localy.

`const onDrop = useCallback((acceptedFiles) => {
    let pdf = acceptedFiles[0];
    if (pdf.type !== "application/pdf") {
      setShowAlert(true);
      setTimeout(() => {
        setShowAlert(false);
      }, 3000);
      return;
    }
    let reader = new FileReader();
    reader.readAsArrayBuffer(pdf);
    let pdfTransformat = new Blob([pdf], { type: pdf.type });
    reader.onload = async function () {
      let user = JSON.parse(localStorage.getItem("user"));
      const cerereInfo = JSON.parse(localStorage.getItem("cerereInfo"));
      let arrayBuffer = pdfTransformat.arrayBuffer(); //this is the variable that I am sending to be saved in the db


const pdfBlob = new Blob([cerere.pdf], {
                      type: "application/pdf",
                    });
                    saveAs(pdfBlob, "cerere");`

This is the code when I am transforming the BLOB saved in the db back to PDF and I try to dowload it. The problem is that the downloaded PDF is corrupted What do I need to change? Is the way that I save the pdf to db wrong or is it how I transform it back to pdf to download it?

0

There are 0 answers