JavaScript Blob

Blobs are immutable objects that represent unprocessed data. The file is a Blob derived from data from the file system. Blobs allow us to create file-like objects on the client that we may transfer to APIs, and these APIs expect URLs rather than needing the server to provide the file. In this article, we are going to discuss the JavaScript Blob with some examples.

What is a JavaScript Blob?

A blob object is simply a collection of bytes that contains data stored in a file. A blob may appear to be a reference to the actual file, but it is not. A blob has the same size and MIME as a simple file. The blob data is stored in the user's memory, and it depends on the browser functions and the blob's size. The file is a derivation of the blob, and it may be used in the same places where the file is used. Blobs are very useful for storing binary data because their content can be easily read as an ArrayBuffer.

Blobs can be stored in the memory or on disk by the web browser, and they can represent truly massive bits of data that are too large to fit in the main memory unless first separated into smaller pieces with the slice () method.

The syntax for creating a blob:

The syntax for creating a JavaScript blob may be defined as:

In this syntax,

  • Blobparts: It is an array of Blob, BufferSource, and string values.
  • Options: It is an optional object.
  • Type: It is a Blob type, generally MIME-type like image.png.

Example:

Let's take an example to understand how to create a JavaScript blob in the program.

Output: After executing this code, we will get the output as shown below in the screenshot.

JavaScript Blob

In this program, we just make a simple <p> element with id = "main".

Blob Object

The blob object is used to represent an immutable blob object that represents raw data. The blob, like a file, has a size and mime-type property.

Example:

Let's take an example to understand how we can create a blob object in the JavaScript.

Output: After executing this code, we will get the output as shown below in the screenshot.

JavaScript Blob

As we have seen, there is a "Click here" button. When we click on the "Click Here" button, it shows the result shown below in the screenshot.

JavaScript Blob

JavaScript Blob URL's

We have Blob URLs that refer to the blob, same as we have file URLs that refer to actual files in the local filesystem. If blob URLs are similar to regular URLs, they can be used everywhere regular URLs can be used. A JavaScript blob may easily be used as a URL for <img> tag and other tags to display its contents. The createObjectURL object can be used to get the blob URL that points to a blob:

Example:

Let's take an example to understand how we can use the blob URL in the program.

Output: After executing this code, we will get the output as shown below in the screenshot.

JavaScript Blob

Here, we can see there is a link. When we click on the link, a text file is a download from the link as shown below in the screenshot:

JavaScript Blob

Blob to base64

Converting a Blob into a base64-encoded string is an alternative to URL.createObjectURL. This encoding protects binary data by encrypting it as a string of ultra-safe "readable" characters with ASCII codes ranging from 0 to 64. More importantly, this encoding can be used in "data-urls". To convert a Blob to base64, we'll use the built-in FileReader object.

A data URL has the following syntax:

Such URLs may be used anywhere, just like "regular" URLs.

Example:

Let's take an example to understand how we can use the blob to base64 in the JavaScript blob.

Output: After executing this code, we will get the output as shown below in the screenshot. When we execute the program, it directly downloads the file at run time.

JavaScript Blob

Pros and Cons of JavaScript blob

There are some pros and cons of JavaScript Blob. Some of them are as follows:

Pros of JavaScript Blob

  1. Blobs are a convenient way to store and reference massive binary data files in a database.
  2. Blobs database backups contain all of the data.
  3. When using Blobs, setting access rights is simple with rights management.

Cons of JavaScript Blob

  1. Blobs are inefficient because of the amount of disk space they need and their time to access them.
  2. All databases don't support blobs.
  3. Due to the large file size of Blobs, creating backups takes a long time.





Latest Courses