如何使用JS将图像显示为Blob对象

Meh*_*ker 1 javascript blob image web-frontend

我的函数在控制台中登录时会返回一个blob对象:

Blob(623853) {name: "profile.jpg", size: 623853, type: "image/jpeg"}
Run Code Online (Sandbox Code Playgroud)

我想用JavaScript向用户显示此图像,我该怎么做?

Aru*_*han 6

尝试

const blobUrl = URL.createObjectURL(blob) // blob is the Blob object
Run Code Online (Sandbox Code Playgroud)

上面的代码行应生成一个Blob URL,您可以将其设置为图像源。

image.src = blobUrl // image is the image element from the DOM
Run Code Online (Sandbox Code Playgroud)