I’m working with Express to create an api that allow to download a PDF file.
Download is working fine, but I don’t see the download window where I choose the folder I want to download, until all “download” is done in network connection.
enter image description here
This pdf is 22Mb.
Here’s part of my code:
const filePath = path.join(__dirname, serverPath.replace(/[/\]/g, path.sep))
const fileStream = fs.createReadStream(filePath)
fileStream
.on('error', (error) => {
next(error) // --> Send error to middleWare
})
.on('end', () => {
logger.info(`${order} - File:${pathDB.fileName} from ${filePath} correctly Downloaded`)
})
// send file in https response, "blob" type
res.setHeader('Content-Disposition', 'attachment; filename=' + pathDB.fileName)
res.setHeader('Content-Type', 'application/octet-stream')
fileStream.pipe(res)
I’ve tried adding Content-Length but doesn’t work.
const stat = fs.statSync(filePath);
const fileSize = stat.size;
res.setHeader('Content-Length', fileSize); // Set the Content-Length header
How should I manage it to allow user to select download folder before all pdf is downloaded?
For example, I’ve seen in AMD drivers website, when you click to download response sent a 200b object, popup the download window, and when you accept, the whole file is downloaded.
https://www.amd.com/en/support/download/drivers.html
Thanks and best regards
Daniel Paredes Ortal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.