将缓冲区从节点 js 转换为图像并在网站上显示

时间:2021-03-21 23:11:19

标签: javascript node.js

我正在尝试从数据库中获取图像,并且设法向客户端发送了一个缓冲区,但是我在将这些缓冲区转换为图像时遇到了问题。

这是我发送给客户端的:

board: Array(8)
0: Array(1)
0:
image:
data: (170354) [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 8, 5, 6, 7, 6, 5, 8, 7, 6, 7, 9, 8, 8, 9, 12, 19, 12, 12, 11, 11, 12, 24, 17, 18, 14, 19, 28, 24, 29, 29, 27, 24, 27, 26, 31, 35, 44, 37, 31, 33, 42, 33, 26, 27, 38, 52, 39, 42, 46, 47, 49, 50, 49, 30, 37, 54, 58, 54, 48, 58, 44, 48, 49, 48, 255, 219, 0, 67, 1, 8, 9, 9, 12, 10, 12, …]
type: "Buffer"
__proto__: Object
__proto__: Object
length: 1
__proto__: Array(0)
1: [{…}]
2: [{…}]
3: [{…}]
4: [{…}]
5: [{…}]
6: [{…}]
7: [{…}]

node.js:

app.get("/mainImages", async function(req, res) {
  try {
    const db = await getDBConnection();
    let ids = await db.all("SELECT id FROM sqp ORDER BY RANDOM() LIMIT 8");
    let resultId = [];
    for (let i = 0; i < 8; i++) {
      resultId.push(ids[i].id);
    }
    let images = [];
    for (let i = 0; i < resultId.length; i++) {
      images.push(await db.all("SELECT image FROM sqp WHERE id=?", resultId[i]));
    }
    console.log(images);
    res.json({"board": images});
    await db.close();
  } catch (error){
    res.status(500).json({"error": "Server is not available right now... Please try again later. "});
  }
});

Javascript:

function processMainImages(respond){
    console.log(respond);
    let firstImageBlock = gen("img");
    let buffer = respond.board[0][0].image.data;
    firstImageBlock.src = "data:image/jpeg;base64," + buffer.toString("base64");
    console.log(firstImageBlock);
    id("myImg").appendChild(firstImageBlock);
  }

错误告诉我 URL 不正确:

net::ERR_INVALID_URL

有人可以帮我写代码吗?

0 个答案:

没有答案