Azure Function HTTP 绑定(bind)从 Azure Blob 存储中读取 Base64 字符串形式的图像。
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQTEhIUEhIUFBUV…K9rk8hCAEkjFMUYiEAI+nHIpsQh0AkisDYRTOiCAbWVtgCtI6IlkHh7LDTQXLH0EIQBj//2Q==
它使用新的缓冲区对其进行转换:
const buf = new Buffer(pictureObj.data.split(",")[1], "base64");
然后它以这种方式返回此缓冲区:
context.bindings.res = {
"status": 200,
"headers": {
"Content-Type": type || "image/jpeg"
},
"body": new Uint8Array(buf)
};
不幸的是,这不起作用。设置“isRaw”也不起作用以及返回缓冲区(buf)本身。错误为 406( Not Acceptable )且正文为空。
问题是这样的:如何通过 HTTP 输出绑定(bind)返回一个 base64 作为二进制图像?
此外,再添加一个 header (例如 Content-Length)会失败并出现以下错误:
info: Worker.Node.2a68d094-3858-406b-a0c5-a81497b3436b[0]
Worker 2a68d094-3858-406b-a0c5-a81497b3436b malformed message invocationResponse.outputData.data.http.headers: string{k:string} expected
[03/12/2017 02:44:32] A ScriptHost error has occurred
[03/12/2017 02:44:32] Error: Choose either to return a promise or call 'done'. Do not use both in your script.
[03/12/2017 02:44:32] Error: Choose either to return a promise or call 'done'. Do not use both in your script.
最佳答案
如果您使用的是 Azure 函数测试版,这应该可以工作:
context.res.setHeader("Content-Type", "image/jpeg")
context.res.raw(new Uint8Array(buf))
此外,当使用 raw 或 send 时,无需调用 context.done,因为它是隐式调用的。
关于node.js - 如何从Azure Function以二进制数据形式返回base64图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47614788/