Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable is a blob in JavaScript

As typeof returns "object"..

var MyBlob = new Blob(['test text'], {type : 'text/plain'}); console.log(typeof MyBlob) // "object" 

is it too early to ask for a generic solution for checking whether or not a variable is a blob as it is not yet widely supported? Or how should I go about testing for blob type in browsers which already have it implemented?

like image 510
Jaak Kütt Avatar asked Dec 22 '13 08:12

Jaak Kütt


People also ask

What does a Blob look like JavaScript?

A blob has its size and MIME just like that of a simple file. The blob data is stored in the memory or filesystem of a user depending on the browser features and size of the blob. A simple blob can be used anywhere we wish just like files.

What does Blob mean in JavaScript?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.

What is Blob in typescript?

Interface BlobA file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.

What is Blob URL in JavaScript?

A URL that was created from a JavaScript Blob can not be converted to a "normal" URL. A blob: URL does not refer to data the exists on the server, it refers to data that your browser currently has in memory, for the current page.


1 Answers

You can test if it is an instanceof Blob like this:

var MyBlob = new Blob(['test text'], {type : 'text/plain'}); console.log(MyBlob instanceof Blob) // true 

jsFiddle: http://jsfiddle.net/jfriend00/5xkgd/

This will work for things that inherit from Blob also.

like image 74
jfriend00 Avatar answered Oct 08 '22 13:10

jfriend00