Skip to content

Instantly share code, notes, and snippets.

@MaxWright
Forked from robnyman/xhr-BlobBuilder.js
Created August 23, 2018 21:37
Show Gist options
  • Select an option

  • Save MaxWright/708c57895c447615c34dbb2086a34dc1 to your computer and use it in GitHub Desktop.

Select an option

Save MaxWright/708c57895c447615c34dbb2086a34dc1 to your computer and use it in GitHub Desktop.

Revisions

  1. @robnyman robnyman revised this gist Feb 28, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion xhr-BlobBuilder.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Create XHR and BlobBuilder
    // Create XHR
    var xhr = new XMLHttpRequest(),
    blob;

  2. @robnyman robnyman revised this gist Feb 28, 2012. 1 changed file with 5 additions and 18 deletions.
    23 changes: 5 additions & 18 deletions xhr-BlobBuilder.js
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,20 @@
    // Create XHR and BlobBuilder
    var xhr = new XMLHttpRequest(),
    blobBuilder = new (window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.OBlobBuilder || window.msBlobBuilder),
    blob;

    xhr.open("GET", "elephant.png", true);
    // Set the responseType to arraybuffer. "blob" is an option too, rendering BlobBuilder unnecessary, but the support for "blob" is not widespread enough yet
    xhr.responseType = "arraybuffer";
    // Set the responseType to blob
    xhr.responseType = "blob";

    xhr.addEventListener("load", function () {
    if (xhr.status === 200) {
    console.log("Image retrieved");

    // File as response
    var response = xhr.response;
    blob = xhr.response;

    if (blobBuilder) {
    // Append the response to the BlobBuilder
    blobBuilder.append(response);
    // Create a blob with the desired MIME type
    blob = blobBuilder.getBlob("image/png");
    }
    else {
    blob = new Blob([response]);
    }

    if (blob) {
    // Put the received blob into IndexedDB
    putElephantInDb(blob);
    }
    // Put the received blob into IndexedDB
    putElephantInDb(blob);
    }
    }, false);
    // Send XHR
  3. @robnyman robnyman created this gist Feb 23, 2012.
    34 changes: 34 additions & 0 deletions xhr-BlobBuilder.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    // Create XHR and BlobBuilder
    var xhr = new XMLHttpRequest(),
    blobBuilder = new (window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.OBlobBuilder || window.msBlobBuilder),
    blob;

    xhr.open("GET", "elephant.png", true);
    // Set the responseType to arraybuffer. "blob" is an option too, rendering BlobBuilder unnecessary, but the support for "blob" is not widespread enough yet
    xhr.responseType = "arraybuffer";

    xhr.addEventListener("load", function () {
    if (xhr.status === 200) {
    console.log("Image retrieved");

    // File as response
    var response = xhr.response;

    if (blobBuilder) {
    // Append the response to the BlobBuilder
    blobBuilder.append(response);
    // Create a blob with the desired MIME type
    blob = blobBuilder.getBlob("image/png");
    }
    else {
    blob = new Blob([response]);
    }

    if (blob) {
    // Put the received blob into IndexedDB
    putElephantInDb(blob);
    }
    }
    }, false);
    // Send XHR
    xhr.send();