javascript - 如何显示从 msal graph API 收到的二进制帐户图像?
问题描述
我可以通过使用查询 msal 图 api,https://graph.microsoft.com/v1.0/me/photo/$value
但是一旦我得到基于 [the documentation][1] 是二进制值的响应,我就无法在我的 Vue 应用程序中实际显示它。我尝试使用 createObjectURL 制作一个 blob 并将其设置为 img 元素的 src 属性,如下所示:
const url = window.URL || window.webkitURL;
const blobUrl = url.createObjectURL(response.data);
document.getElementById("imageElement").setAttribute("src", blobUrl);
但我得到了错误:
TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed.
我得到这样的图像:
axios.get("https://graph.microsoft.com/v1.0/me/photos/48x48/$value", {
headers: {
Authorization: "Bearer " + "xxxxxxx" // this is the auth token
}
})
.then(response => {
const url = window.URL || window.webkitURL;
const blobUrl = url.createObjectURL(response.data);
document.getElementById("imageElement").setAttribute("src", blobUrl);
});
```
[1]: https://docs.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0
解决方案
试试这个。
yourImg.src = 'data:image/jpeg;base64, ' + Buffer.from(response.data, 'binary').toString('base64');
还可以在这里查看答案和评论
推荐阅读
- jenkins - 以 XML 格式将 Jacoco 的覆盖率报告导出到 Jenkins
- python - 如何在 Python 中绘制平均 ROC 和 AUC?
- routing - 路由模式匹配未从多个路由调用
- ansible - ansible 创建本地目录(主)
- flutter - Flutter:带有表情符号的 TextField 无法正常工作
- android - 如何获取 JSON 数组的正确 URL
- css - 设置溢出后有边距:隐藏的div?
- python - djangorestframework 视图的查询参数的 DRY 验证
- c# - 如何在 EF Core 中拥有外键和其他属性的复合键?
- javascript - 如何在 react-numpad 小部件中使用 handleChange?