Rename Pdf file before opening for preview

Asked

Viewed 210 times

2

I am receiving a PDF file in the Answer of a POST request, and on the server side I have configured the file name and everything else. But now I’m visualizing it in the browser instead of downloading it and it no longer obeys the name placed on the server and is assigning another name, like this: "2050102b-7041-4cfa-9ebf-0c05af158005" and I’m not getting it to receive a specific name Besides the name being weird, the URL in the browser looks like this: "blob:http://localhost:8080/2050102b-7041-4cfa-9ebf-0c05af158005"

Server:

stream.on('finish', function() {
    if (fs.existsSync('C:/Projetos/Relatorios/pdfs/Tabela.pdf')) {
        let options = {
            root: '../pdfs/',
            dotfiles: 'deny'
        };
        let fileName = 'Tabela.pdf';
        res.sendFile(fileName, options, function (err) {
            if (err){
                console.log(err)
            }else{
                console.log('Arquivo enviado!')
            }
        });
    }
})

Client:

let link = document.createElement('a')    
link.target = '_blank'
let url = window.URL.createObjectURL(blob);
link.href = url .split('/')[0] + '/' + url .split('/')[1].replace(/.*/, 'meuarquivo.pdf');

Server: Express 4.15.0

Client: Vuejs/Quasar-Framework

1 answer

0


Try to set the header of the content type server response to PDF and the Content-disposition name to the file name. Before the res.sendFile line something like:

res.set({
    'Content-disposition', `attachment; filename=${fileName}`,
    'Content-type', 'application/pdf'
})

Browser other questions tagged

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