Last active
January 25, 2024 10:42
-
-
Save InsilicoSoft/17e5213d0a54f78a089eb26f10445ee6 to your computer and use it in GitHub Desktop.
Stream local pdf file from nodejs (restify) server to client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.get('/downloadPdf/:fileData', function (req, res) { | |
// config | |
var fileData = Buffer.from(req.params.fileData, 'base64'); | |
var menuData = JSON.parse(fileData.toString()); | |
var userName = menuData.userName; | |
var menuName = slug(menuData.menuName); | |
var fileName = userName + "-" + menuName + PDF_EXT; | |
var filePath = PDF_PATH + fileName; | |
// process headers | |
res.writeHead(200, { | |
"Content-Type": 'application/octet-stream', | |
"Content-Disposition": "attachment; filename=" + fileName | |
}); | |
// process Data | |
var readStream = fileSystem.createReadStream(filePath); | |
readStream.pipe(res); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment