Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 940137 of Blob

  • Revision slug: Web/API/Blob
  • Revision title: Blob
  • Revision id: 940137
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment add entries for close()/isClosed

Revision Content

{{APIRef("File API")}}

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

An easy way to construct a Blob is by invoking the {{domxref("Blob.Blob", "Blob()")}} constructor. Another way is to use the slice() method to create a blob that contains a subset of another blob's data.

Constructor

{{domxref("Blob.Blob", "Blob(blobParts[, options])")}}
Returns a newly created Blob object whose content consists of the concatenation of the array of values given in parameter.

Properties

{{domxref("Blob.isClosed")}} {{readonlyinline}}
A boolean value, indicating whether the {{domxref("Blob.close()")}} method has been called on the blob. Closed blobs can not be read.
{{domxref("Blob.size")}} {{readonlyinline}}
The size, in bytes, of the data contained in the Blob object.
{{domxref("Blob.type")}} {{readonlyinline}}
A string indicating the MIME type of the data contained in the Blob. If the type is unknown, this string is empty.

Methods

{{domxref("Blob.close()")}}
Closes the blob object, possibly freeing underlying resources.
{{domxref("Blob.slice()", "Blob.slice([start[, end[, contentType]]])")}}
Returns a new Blob object containing the data in the specified range of bytes of the source Blob.

Examples

Blob constructor example usage

The following code:

var aFileParts = ['<a id="a"><b id="b">hey!</b></a>'];
var oMyBlob = new Blob(aFileParts, {type : 'text/html'}); // the blob

is equivalent to:

var oBuilder = new BlobBuilder();
var aFileParts = ['<a id="a"><b id="b">hey!</b></a>'];
oBuilder.append(aFileParts[0]);
var oMyBlob = oBuilder.getBlob('text/xml'); // the blob

The {{domxref("BlobBuilder")}} interface offers another way to create Blobs, but is now deprecated and should no longer be used.

Example for creating a URL to a typed array using a blob

The following code:

var typedArray = GetTheTypedArraySomehow();
var blob = new Blob([typedArray], {type: 'application/octet-binary'}); // pass a useful mime type here
var url = URL.createObjectURL(blob);
// url will be something like: blob:d3958f5c-0777-0845-9dcf-2cb28783acaf
// now you can use the url in any context that regular URLs can be used in, for example img.src, etc.

Example for extracting data from a Blob

The only way to read content from a Blob is to use a {{domxref("FileReader")}}. The following code reads the content of a Blob as a typed array.

var reader = new FileReader();
reader.addEventListener("loadend", function() {
   // reader.result contains the contents of blob as a typed array
});
reader.readAsArrayBuffer(blob);

By using other methods of {{domxref("FileReader")}}, it is possible to read the contents of a Blob as a string or a data URL.

Specifications

Specification Status Comment
{{SpecName('File API','#blob','Blob')}} {{Spec2('File API')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 5[1] 4[2] 10 11.10[1] 5.1[1]
slice() 10 {{property_prefix("webkit")}}
21
5 {{property_prefix("moz")}}[3]
13
10 12 5.1 {{property_prefix("webkit")}}
Blob() constructor 20 {{CompatGeckoDesktop("13.0")}} 10 12.10 6
close() and isClosed {{CompatUnknown}} {{CompatNo}} {{bug("1048325")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatGeckoMobile("13.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
slice() {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Blob() constructor {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
close() and isClosed {{CompatUnknown}} {{CompatNo}} {{bug("1048325")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

[1] A version of slice() taking the length as second argument was implemented in WebKit and Opera 11.10. However, since that syntax differed from {{jsxref("Array/slice", "Array.slice()")}} and {{jsxref("String/slice", "String.slice()")}}, WebKit removed support and added support for the new syntax as Blob.webkitSlice().

[2] A version of slice() taking the length as second argument was implemented in Firefox 4. However, since that syntax differed from {{jsxref("Array/slice", "Array.slice()")}} and {{jsxref("String/slice", "String.slice()")}}, Gecko removed support and added support for the new syntax as mozSlice().

[3] Prior to Gecko 12.0 {{geckoRelease("12.0")}}, there was a bug that affected the behavior of slice(); it did not work for start and end positions outside the range of signed 64-bit values; it has now been fixed to support unsigned 64-bit values.

Note: The slice() method had initially taken length as the second argument to indicate the number of bytes to copy into the new Blob. If you specified values such that start + length exceeded the size of the source Blob, the returned Blob contained data from the start index to the end of the source Blob.

Note: Be aware that the slice() method has vendor prefixes on some browsers and versions: blob.mozSlice() for Firefox 12 and earlier and blob.webkitSlice() in Safari. An old version of the slice() method, without vendor prefixes, had different semantics, and is obsolete. The support for blob.mozSlice() has been dropped with Firefox 30.

Gecko notes: availability in privileged code

To use from chrome code, JSM and Bootstrap scope, you have to import it like this:

Cu.importGlobalProperties(['Blob']);

Blob is available in Worker scopes.

See also

Revision Source

<p>{{APIRef("File API")}}</p>

<p>A <code>Blob</code> object represents a file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The {{domxref("File")}} interface is based on <code>Blob</code>, inheriting blob functionality and expanding it to support files on the user's system.</p>

<p>An easy way to construct a <code>Blob</code> is by invoking the {{domxref("Blob.Blob", "Blob()")}} constructor. Another way is to use the <code>slice()</code> method to create a blob that contains a subset of another blob's data.</p>

<h2 id="Constructor">Constructor</h2>

<dl>
 <dt>{{domxref("Blob.Blob", "Blob(blobParts[, options])")}}</dt>
 <dd>Returns a newly created <code>Blob</code> object whose content consists of the concatenation of the array of values given in parameter.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{domxref("Blob.isClosed")}} {{readonlyinline}}</dt>
 <dd>A boolean value, indicating whether the {{domxref("Blob.close()")}} method has been called on the blob. Closed blobs can not be read.</dd>
 <dt>{{domxref("Blob.size")}} {{readonlyinline}}</dt>
 <dd>The size, in bytes, of the data contained in the <code>Blob</code> object.</dd>
 <dt>{{domxref("Blob.type")}} {{readonlyinline}}</dt>
 <dd>A string indicating the MIME&nbsp;type of the data contained in the <code>Blob</code>. If the type is unknown, this string is empty.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("Blob.close()")}}</dt>
 <dd>Closes the blob object, possibly freeing underlying resources.</dd>
 <dt>{{domxref("Blob.slice()", "Blob.slice([start[, end[, contentType]]])")}}</dt>
 <dd>Returns a new <code>Blob</code> object containing the data in the specified range of bytes of the source <code>Blob</code>.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<h3 id="Blob_constructor_example_usage">Blob constructor example usage</h3>

<p>The following code:</p>

<pre class="brush: js">
var aFileParts = ['&lt;a id="a"&gt;&lt;b id="b"&gt;hey!&lt;/b&gt;&lt;/a&gt;'];
var oMyBlob = new Blob(aFileParts, {type : 'text/html'}); // the blob
</pre>

<p>is equivalent to:</p>

<pre class="brush: js">
var oBuilder = new BlobBuilder();
var aFileParts = ['&lt;a id="a"&gt;&lt;b id="b"&gt;hey!&lt;/b&gt;&lt;/a&gt;'];
oBuilder.append(aFileParts[0]);
var oMyBlob = oBuilder.getBlob('text/xml'); // the blob
</pre>

<div class="warning">
<p>The {{domxref("BlobBuilder")}} interface offers another way to create <code>Blob</code>s, but is now deprecated and should no longer be used.</p>
</div>

<h3 id="Example_for_creating_a_URL_to_a_typed_array_using_a_blob">Example for creating a URL to a typed array using a blob</h3>

<p>The following code:</p>

<pre class="brush: js">
var typedArray = GetTheTypedArraySomehow();
var blob = new Blob([typedArray], {type: 'application/octet-binary'}); // pass a useful mime type here
var url = URL.createObjectURL(blob);
// url will be something like: blob:d3958f5c-0777-0845-9dcf-2cb28783acaf
// now you can use the url in any context that regular URLs can be used in, for example img.src, etc.
</pre>

<h3 id="Example_for_extracting_data_from_a_Blob">Example for extracting data from a Blob</h3>

<p>The only way to read content from a Blob is to use a {{domxref("FileReader")}}. The following code reads the content of a Blob as a typed array.</p>

<pre class="brush: js">
var reader = new FileReader();
reader.addEventListener("loadend", function() {
   // reader.result contains the contents of blob as a typed array
});
reader.readAsArrayBuffer(blob);</pre>

<p>By using other methods of {{domxref("FileReader")}}, it is possible to read the contents of a Blob as a string or a data URL.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('File API','#blob','Blob')}}</td>
   <td>{{Spec2('File API')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>5<sup>[1]</sup></td>
   <td>4<sup>[2]</sup></td>
   <td>10</td>
   <td>11.10<sup>[1]</sup></td>
   <td>5.1<sup>[1]</sup></td>
  </tr>
  <tr>
   <td><code>slice()</code></td>
   <td>10 {{property_prefix("webkit")}}<br />
    21</td>
   <td>5 {{property_prefix("moz")}}<sup>[3]</sup><br />
    13</td>
   <td>10</td>
   <td>12</td>
   <td>5.1 {{property_prefix("webkit")}}</td>
  </tr>
  <tr>
   <td><code>Blob()</code> constructor</td>
   <td>20</td>
   <td>{{CompatGeckoDesktop("13.0")}}</td>
   <td>10</td>
   <td>12.10</td>
   <td>6</td>
  </tr>
  <tr>
   <td><code>close()</code> and <code>isClosed</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}} {{bug("1048325")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE&nbsp;Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("13.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>slice()</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Blob()</code> constructor</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>close()</code> and <code>isClosed</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}} {{bug("1048325")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] A version of <code>slice()</code> taking the length as second argument was implemented in <a href="https://trac.webkit.org/changeset/55670">WebKit</a> and <a href="https://www.opera.com/docs/specs/presto28/file/#blob">Opera 11.10</a>. However, since that syntax differed from {{jsxref("Array/slice", "Array.slice()")}} and {{jsxref("String/slice", "String.slice()")}}, WebKit removed support and added support for the new syntax as <a href="https://trac.webkit.org/changeset/83873"><code>Blob.webkitSlice()</code></a>.</p>

<p>[2] A version of <code>slice()</code> taking the length as second argument was implemented in <a href="https://hg.mozilla.org/mozilla-central/rev/1b3947ed93c6">Firefox 4</a>. However, since that syntax differed from {{jsxref("Array/slice", "Array.slice()")}} and {{jsxref("String/slice", "String.slice()")}}, Gecko removed support and added support for the new syntax as <code>mozSlice()</code>.</p>

<p>[3] Prior to Gecko 12.0 {{geckoRelease("12.0")}}, there was a bug that affected the behavior of <code>slice()</code>; it did not work for <code>start</code> and <code>end</code> positions outside the range of signed 64-bit values; it has now been fixed to support unsigned 64-bit values.</p>

<dl>
</dl>

<div class="note">
<p><strong>Note:</strong> The <code>slice()</code> method had initially taken <code>length</code> as the second argument to indicate the number of bytes to copy into the new <code>Blob</code>. If you specified values such that <code>start + length</code> exceeded the size of the source <code>Blob</code>, the returned <code>Blob</code> contained data from the start index to the end of the source <code>Blob</code>.</p>
</div>

<div class="note"><strong>Note:</strong> Be aware that the <code>slice()</code> method has vendor prefixes on some browsers and versions: <code>blob.mozSlice()</code> for Firefox 12 and earlier and <code>blob.webkitSlice()</code> in Safari. An old version of the <code>slice()</code> method, without vendor prefixes, had different semantics, and is obsolete. The support for <code>blob.mozSlice()</code> has been dropped with Firefox&nbsp;30.</div>

<h3 id="Gecko_notes_availability_in_privileged_code">Gecko notes: availability in privileged code</h3>

<p>To use from chrome code, JSM and Bootstrap scope, you have to import it like this:</p>

<pre class="brush: js">
Cu.importGlobalProperties(['Blob']);
</pre>

<p><code>Blob</code> is available in Worker scopes.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("BlobBuilder")}}</li>
 <li>{{domxref("File")}}</li>
 <li>{{domxref("URL.createObjectURL")}}</li>
 <li><a href="/en-US/docs/Components.utils.importGlobalProperties">Components.utils.importGlobalProperties</a></li>
</ul>
Revert to this revision