格式化Express zip的缓冲区数据

问题描述

我试图发送多个pdf文件以响应我的mongo实例,并且在处理bson缓冲区取得了很多成功之后,我决定使用express zip来存储所有路径,并且为了理解这样做您必须将数据作为Uint8Array发送。所以我尝试了以下代码,并收到以下控制台错误。有关如何仅发送缓冲区以进行响应的任何想法,这样我就可以在那儿更轻松地处理它们。

单个bson对象

[0]   {
     _id: 5f4e705d8572b50988d15011,file_data: Binary {
       _bsontype: 'Binary',sub_type: 0,position: 443766,buffer: <Buffer 25 50 44 46 2d 31 2e 37 0d 31 20 30 20 6f 62 6a 0d 3c 3c 2f 54 79 70 65 20 2f 58 4f 62 6a 65 63 74 20 2f 53 75 62 74 79 70 65 20 2f 49 6d 61 67 65 20 ... 443716 more bytes>
     },filename: 'Scan_0020 (1).pdf',date: '9/1/2020'
  },

代码

router.get("/invoices/new",auth,async (req,res) => {
  const conn = mongoose.connection;
  const gfs = Grid(conn.db,mongoose.mongo);

  const period = JSON.parse(req.query.q);

  const { periodStart,periodEnd } = period;

  gfs.files
    .find({
      date: { $gte: periodStart,$lte: periodEnd },})
    .toArray(function (err,files) {
      if (err) {
        res.json(err);
      }

      const bufs = [];
      files.map((f,i) => {
        var arrayBuffer = new Uint8Array(f.file_data.buffer);

        bufs[i]
          ? (bufs[i].path = arrayBuffer)((bufs[i].filename = f.filename))
          : (bufs[i] = {
              filename: f.filename,path: arrayBuffer,});
      });

      res.zip(bufs);
    });
});

错误

[0] TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received Uint8Array(167263) [.....

我想发送缓冲区和文件名,以便我可以循环iframe反应。任何帮助将不胜感激

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)