Aaron Aaron - 2 months ago 14
Node.js Question

getting node.js to send buffer as a file

I'm trying to create an excel workbook on the server side that is send to the user when they visit a certain route (using express.js)
I'm using the module from https://github.com/riyadhalnur/excelbuilderjs-node
and I do have a large chunk of data in the

workBookB64
constant, and in my browser (chrome) I do get both headers for disposition and content type after the
res.end()
call. BUT the file is never downloaded on the browser side.
I have my node process running behind an Nginx reverse proxy, to handle the SSL.
Any ideas on how to get the file to download would be great!

const result = Excel.createFile( workBook );
const workBookB64 = new Buffer( result, 'base64' );

res.setHeader( 'Content-Disposition', 'attachment; filename=' + fileName );
res.setHeader( 'Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64' );
res.end( workBookB64 );


Down vote and not even a comment? I feel so dirty and used now. :(

Answer

I solved this on my own. It turns out that the issue was in how I was requesting the URL. The client side code was using a jQuery call: $.get("/getReport") so the response was going back to this call, which had no callback handler. So to me it appeared as if it wasn't working. The solution I used was to eliminate the JS, and just use an anchor tag, and call the URL directly.

Note: If you feel the need to down vote my answer too, at least leave a comment.

Comments