วัตถุ blob ใช้สำหรับแสดงวัตถุ blob ซึ่งไม่เปลี่ยนรูปและใช้สำหรับแสดงข้อมูลดิบ Blob มีคุณสมบัติขนาดและประเภท mime เหมือนกับที่ไฟล์มี File มาจาก Blob และ Blob สามารถใช้ได้ในสถานที่ที่มีการใช้ไฟล์
ต่อไปนี้เป็นรหัสที่แสดงวัตถุหยดใน JavaScript -
ตัวอย่าง
<!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> ผลลัพธ์
รหัสข้างต้นจะสร้างผลลัพธ์ต่อไปนี้ -

เมื่อคลิกปุ่ม 'คลิกที่นี่' -
