将文件发送为缓冲区

发布于 2025-01-28 16:13:21 字数 607 浏览 0 评论 0原文

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);

为什么不下载文件? 尝试从JSReport下载某些内容,但是如果我记录buffer,它会给我奇怪的字符

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2025-02-04 16:13:21

aneesh ,返回缓冲区是理想的,

 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');

您也可以使用buffer.from()功能。

我希望它能有所帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文