使用nodejs将base64Image上传到Azure存储中的错误

问题描述

将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....
  1. 代码
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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...