[英]How to read json file from storage blob container with azure function using javascript?
[英]How to download png file from stringstream content azure blob storage using javascript
我写了这段代码来获取数据,我得到了这样的响应
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data.toString());
});
readableStream.on("end", () => {
resolve(chunks.join(""));
});
readableStream.on("error", reject);
});
}
if (type == "download") {
const name = req.query.name || req.body.name;
const itemname = req.query.itemname || req.body.itemname;
var container = name ? name : "securesharecc";
const containerClient = await blobServiceClient.getContainerClient(container);
const blobName = itemname ? itemname : "sample.png";
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const downloadBlockBlobResponse = await blockBlobClient.download(0);
console.log("\nDownloaded blob content...");
var a = await streamToString(downloadBlockBlobResponse.readableStreamBody);
console.log("\t", a);
context.res = {
body: { data: a }
};
context.done();
}
因此,此代码使用 azure 容器中的项目名称下载文件。
但我收到这样的回复
{
"data": "PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0002�\u0000\u0000\u0001�\b\u0002\u0000\u0000\u0000\u000f�\u001fl\u0000\u0000\u0000\u0001sRGB\u0000��\u001c�\u0000\u0000\u0000\u0004gAMA\u0000\u0000��\u000b ....."
}
我想直接下载这个文件而不是显示内容,怎么做?
请尝试将您的streamToString
方法更改为以下内容:
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
let chunks = Buffer.from([]);
readableStream.on("data", (data) => {
chunks = Buffer.concat([chunks, data], chunks.length + data.length);
});
readableStream.on("end", () => {
resolve(chunks);
});
readableStream.on("error", reject);
});
}
本质上,图像文件是二进制文件,您不应该尝试将其数据转换为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.