gpt4 book ai didi

node.js - 使用nodejs将base64Image上传到azure存储时出错

转载 作者:行者123 更新时间:2023-12-03 05:33:22 25 4
gpt4 key购买 nike

将 base64Image 上传到 Azure 时遇到错误。

使用 Node.js 编写代码:

var name = req.body.name;
var img = req.body.image; //this image is base64Image

var uploadOptions = {
container: 'myphoto',
blob: name,
text: img
}

blobServiceClient.createBlockBlobFromText(
uploadOptions.container,
uploadOptions.blob,
uploadOptions.text,
{
contentType: 'image/jpg',
contentEncoding: 'base64'
},
function(error, result, response) {
if (error) {
res.send(error);
}

console.log("result", result);

console.log("response", response);
}
);

这是 Azure 中的图像: the result that I got in Azure

这是我打开图像时遇到的错误: the error that I got when I opened the image

请求的图像采用base64Image格式。

如何修复此错误?

最佳答案

如果您将base64图像上传到azure blob存储,我建议您将图像内容转换为Buffer然后上传。

例如

我的 Base64 图像

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUTExMWFRUXGBobGBgYGRsfIBoZIBsgHSAgIB0ZISghGR0lGx0XIT....
  • 代码
  • var name = req.body.name;
    var img = req.body.image; //this image is base64Image
    var matches = img.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);
    var type = matches[1];
    var buffer = Buffer.from(matches[2], "base64");
    var uploadOptions = {
    container: 'test',
    blob: name,
    text: buffer
    }
    blobServiceClient.createBlockBlobFromText(
    uploadOptions.container,
    uploadOptions.blob,
    uploadOptions.text,
    {
    contentSettings: {
    contentType: type,
    },
    },
    function(error, result, response) {
    if (error) {
    res.send(error);
    }

    console.log("result", result);

    console.log("response", response);
    }
    );

    我的结果。 enter image description here

    关于node.js - 使用nodejs将base64Image上传到azure存储时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64080539/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com