在 Node.js 中,可以使用 Buffer 对象将 Uint8Array 类型的数据转换为二进制数据,然后使用 res.send() 方法将二进制数据发送给前端。以下是一个示例,演示如何将 Uint8Array 类型的数据以文件形式发送给前端:

const express = require('express');

const app = express();

app.get('/file', (req, res) => {
  // 创建 Uint8Array 类型的数据
  const data = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64]);

  // 转换为 Buffer 对象
  const buffer = Buffer.from(data);

  // 发送文件
  res.setHeader('Content-Type', 'application/octet-stream');
  res.setHeader('Content-Disposition', 'attachment; filename="file.bin"');
  res.send(buffer);
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});
最后修改:2023 年 04 月 11 日 07 : 08 PM
如果觉得我的文章对你有用,请随意赞赏