How to convert an Image to blob using JavaScript?

Following is the code to convert an image to blob using JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-size: 18px;
      font-weight: 500;
      color: rebeccapurple;
<h1>Convert an Image to blob using JavaScript</h1>
<div class="sample">
<img src="" />
<button class="Btn">Convert</button>
<div class="result"></div>
<h3>Click on the above button to convert the above image to blob</h3>
   let BtnEle = document.querySelector(".Btn");
   let resEle = gdocument.querySelector(".result");
   BtnEle.addEventListener("click", () => {
      .then(function (response) {
         return response.blob();
      .then(function (blob) {
         resEle.innerHTML = "blob.size = " + blob.size + "<br>";
         resEle.innerHTML += "blob.type = " + blob.type + "<br>";


The above code will produce the following output −

On clicking the ‘Convert’ button −

Updated on: 17-Jul-2020

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started