Send file as buffer

Viewed 155
const resp = await client.render(data);
const Writable = require('stream').Writable;
var buffer = [];
const myWritable = new Writable({
 write(chunk, encoding, callback) {
   console.log(encoding);
  buffer += chunk;
   callback();
 },
 });
myWritable.on('finish', () => {
 res.writeHead(200, {
    'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
   'Content-Disposition': `inline; filename=Non Billed Jobs.xlsx`,
   });
   res.end(buffer);
 });
resp.pipe(myWritable);

Why doesn’t this download the file? Trying to download something from jsreport but if I log buffer, it gives me strange characters

1 Answers

As suggested by Aneesh, returning a buffer would be ideal

 res.set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 res.set('Content-Disposition', 'inline; filename=Non Billed Jobs.xlsx');
 res.send(new Buffer(BUFFER_DATA, 'base64');

You could also use the Buffer.from() function instead.

I hope it helped!

Related