【发布时间】:2022-01-12 07:44:56
【问题描述】:
我已经编写了这段代码来获取数据,我得到了这样的响应
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 base64string = await streamToString(downloadBlockBlobResponse.readableStreamBody);
console.log("\t", base64string);
context.res = {
body: { data: base64string }
};
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 ....."
}
如果我想直接下载这个文件而不是显示内容,该怎么编码,怎么做?
【问题讨论】:
-
您希望在响应中看到什么?也就是说,
a在{data: a}中应该是什么? -
这里是base64string
标签: javascript node.js typescript azure