gpt4 book ai didi

node.js - npm azure-storage 下载文件为 base64

转载 作者:太空宇宙 更新时间:2023-11-04 00:09:10 24 4
gpt4 key购买 nike

我正在使用这个package将文件从 azure 下载到本地 Node 服务器。但我不想将文件保存在本地服务器中然后读取它以转换为base64,我想直接将其转换为base64。我怎样才能实现这个目标?

 this.blobService.getBlobToStream('pictures', path, fs.createWriteStream(`./src/temp/${filename}.jpeg`), function(error, result, response) {
if (!error) {
resolve({ message: `Items in container pictures:`, data: result});
} else {
reject(error);
}
});

最佳答案

内置Node仅支持ecnode Buffer类型到base64,因为你不想将blob下载到本地服务器,所以你必须使用外部模块来编码

npm install base64-stream 然后使用下面的代码。

var base64 = require('base64-stream');
...
this.blobService.createReadStream(container, path).pipe(base64.encode()).pipe(res);

请注意,在您的 later question 中,您可以使用

指定 Content-Type
res.header('Content-Type', properties['contentType']);
应省略

Content-Type 设置。在我的测试中,如果Content-Type设置成功,图像将作为附件下载,而不是显示为内联内容。您的代码成功是因为它实际上将 Content-Type 设置为 undefined

您使用的属性是带有 blob 信息的 BlobResult(Json 格式)

BlobResult {
container: xxx,
...
contentSettings:
{ contentType: 'image/jpg',
contentMD5: 'uNkt2WJ75X13TjQbfY7qkA==' },
...
}

因此 contentType 应该以这种格式使用 properties.contentSettings.contentType

关于node.js - npm azure-storage 下载文件为 base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735464/

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