成功将 base64 图像保存到 Azure Blob 存储但 blob 图像始终 broken/small 白框

Successfully Saving base64 Image to Azure Blob storage but blob image always broken/small white box

我有一张转换为 base64 的图像,我正在尝试使用 createBlockBlobFromText 上传到我的 Azure blob 存储。

self.blobServer.createBlockBlobFromText(containerName, fileName, baseStrChars, { contentSettings: { contentType: 'image/jpeg' } }, function(error, result, response) {
     if (error) {
         console.log(error);
     }
     console.log("result", result);
     console.log("response", response);
});

我的新 jpeg 图像出现在 blob 存储容器中,但是当我转到 blob 图像时 URL 我总是得到 this

我的容器的访问策略设置为容器,我将我的 base64 字符串粘贴到 base64 到图像转换器,然后出现了正确的图像。问题似乎是我创建 blob 的方式而不是 base64 字符串。

我很困惑为什么整个流程似乎都在工作,但是当我转到 URL 时图像仍然损坏。有什么想法吗?

要在浏览器中通过 Urls 直接访问图像需要二进制内容。您可以在 node.js 后端将 base64 编码字符串转换为二进制缓冲区,并将缓冲区字符串上传到 Azure 存储。

请尝试以下代码片段:

var rawdata = 'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
var matches = rawdata.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);
var type = matches[1];
var buffer = new Buffer(matches[2], 'base64');

blobsrv.createBlockBlobFromText('mycontainer','profile-pic-123.jpg', buffer, {contentType:type}, function(error, result, response) {
        if (error) {
            console.log(error);
        }else{
         console.log(result)
        }
    });