Downloading PDF file from node - I recieve blank pages

In my React app I’m trying to download pdf file from my server to user download folder. But instead of normal pdf I recieve only blank pages. What can be the problem?
My frontend download function:

import download from 'downloadjs';

axios.get('http://localhost:3001/downloadpdftodisk', { params: { filename } })
                .then((res) => {
                    download(res.data, 'test.pdf');

Server part:

app.get("/downloadpdftodisk", async (req, res) => {

  
  const file = __dirname + '/../' + req.query.filename;

    res.download(file, req.query.filename);

})
1 Like

Turns out I had to force to recieve blob format. In case anyone will be stuck with the same problem:
Frontend:

axios.post('http://localhost:3001/downloadpdf', {params})
         .then((res) => {
             if (res) {
                 let filename = res.data;
                axios('http://localhost:3001/downloadpdftodisk', {
                    method: 'GET',
                    responseType: 'blob',
                    params: { filename }
                })
                .then((res) => {
                    console.log(res);
                    const file = new Blob(
                        [res.data], 
                        {type: 'application/pdf'});
                        const fileURL = URL.createObjectURL(file);
                        download(fileURL);
                })

Server:

app.get("/downloadpdftodisk", async (req, res) => {
  const loc = __dirname + '/../' + req.query.filename;
  var filestream = fs.createReadStream(loc);
  filestream.pipe(res);
})

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.