Skip to content

Instantly share code, notes, and snippets.

@InsilicoSoft
Last active January 25, 2024 10:42
Show Gist options
  • Save InsilicoSoft/17e5213d0a54f78a089eb26f10445ee6 to your computer and use it in GitHub Desktop.
Save InsilicoSoft/17e5213d0a54f78a089eb26f10445ee6 to your computer and use it in GitHub Desktop.
Stream local pdf file from nodejs (restify) server to client
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