Computer >> Computer tutorials >  >> Programming >> Javascript

Blob object in JavaScript


The blob object is used for representing a blob object which is immutable and is used for representing raw data. The blob has a size and mime type property just like a file has. File is a derivation of blob and blob can be used in places where the file is used.

Following is the code showing blob object in JavaScript −

Example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .result{
      font-size: 18px;
      font-weight: 500;
      color: rebeccapurple;
   }
</style>
</head>
<body>
<h1>Blob object in JavaScript</h1>
<div class="result"></div>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to create and display a blob object</h3>
<script>
   let resEle = document.querySelector(".result");
   document.querySelector(".Btn").addEventListener("click", () => {
      let blob = new Blob(["This is a sample blob"], { type: "text/plain" });
      resEle.innerHTML = "blob.type = " + blob.type + "<br>";
      resEle.innerHTML += "blob.size = " + blob.size;
   });
</script>
</body>
</html>

Output

The above code will produce the following output −

Blob object in JavaScript

On clicking the ‘CLICK HERE’ button −

Blob object in JavaScript