Configuration of Response Headers

Asked

Viewed 183 times

2

I created an Express server and my client in Quasar/Vuejs. I need to send a PDF to my client, the PDF in question is already created on the server, but I am facing problems to send it to the client. I am using Axios to make client-side requests. Follow the code below:

Client:

axios({
      url: '/server/gerarpdf',
      method: 'post',
      data: this.pessoas,
      type: 'application/json',
      responseType: 'application/pdf'})
    .then(function (response) {
      let file = new Blob([response.data], {type: 'application/pdf'})
      let fileURL = URL.createObjectURL(file)
      window.open(fileURL)
    })
    .catch(function (error) {
      console.log(error)
    })

Server:

res.setHeader('content-type', 'application/pdf');
    res.setHeader('content-disposition', 'attachment; filename=Tabela.pdf');
    let filePath = "C:/Projetos/Relatorios/pdfs/Tabela.pdf";
    let fileName = 'PDF.pdf'; //
    res.download(filePath, fileName, function (err, results) {
        if(err){
            console.log(err)
        }else {
            console.log('Baixou!!')
        }
    });

Before the PDF did not open, now it opens but is empty, and when I open it manually all the contents of it appears normally. My question is: Which Headers should I set up on both the server and client?? And how do I make sure that when it opens I don’t get the Blob address I’m using, like this: "bb560c1c-5d9e-429a-8330-80e2e55e0a42"??

Thanks in advance

  • What version of Express are you using? Looking in the documentation I can’t find anything to say that you have to set the headers. The .download doesn’t do that already?

  • I’m using version 4.15.0, and I’ve tried several things and nothing is working, at least I need to configure the Headers on the client side, which I’m also unable to do so that the PDF download occurs.

  • Take a look at this https://stackoverflow.com/a/7288883/2993657. I believe you can help.

  • I already managed to make the download take place just now, using another code, I appreciate your help, however, the PDF is coming corrupted.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.