使用 nodejs 将 base64Image 上传到 azure 存储时出错
error in uploading base64Image to azure storage by using nodejs
我在将 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 中的图像:
这是我打开图片时遇到的错误:
请求的图像是 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);
}
);
我的结果。
我在将 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 中的图像:
这是我打开图片时遇到的错误:
请求的图像是 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);
}
);
我的结果。