Rectangle 27 0

javascript Blob from DataURL?


Thanks for the link it looks like a great workaround for my needs. I wonder if the charset will work as part of the mimetype.

Note
Rectangle 27 0

javascript Blob from DataURL?


function dataURItoBlob(dataURI) {
    if(typeof dataURI !== 'string'){
        throw new Error('Invalid argument: dataURI must be a string');
    }
    dataURI = dataURI.split(',');
    var type = dataURI[0].split(':')[1].split(';')[0],
        byteString = atob(dataURI[1]),
        byteStringLength = byteString.length,
        arrayBuffer = new ArrayBuffer(byteStringLength),
        intArray = new Uint8Array(arrayBuffer);
    for (var i = 0; i < byteStringLength; i++) {
        intArray[i] = byteString.charCodeAt(i);
    }
    return new Blob([intArray], {
        type: type
    });
}
Note
Rectangle 27 0

javascript Blob from DataURL?


dataURItoBlob : function(dataURI, dataTYPE) {
        var binary = atob(dataURI.split(',')[1]), array = [];
        for(var i = 0; i < binary.length; i++) array.push(binary.charCodeAt(i));
        return new Blob([new Uint8Array(array)], {type: dataTYPE});
    }

input dataURI is Data URL and dataTYPE is the file type and then output blob object

Note
Rectangle 27 0

javascript Blob from DataURL?


function dataURItoBlob(dataURI) {
    var arr = dataURI.split(','), mime = arr[0].match(/:(.*?);/)[1];
    return new Blob([atob(arr[1])], {type:mime});
}
Note
Rectangle 27 0

javascript Blob from DataURL?


function dataURItoBlob(dataURI) {
  // convert base64 to raw binary data held in a string
  // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
  var byteString = atob(dataURI.split(',')[1]);

  // separate out the mime component
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

  // write the bytes of the string to an ArrayBuffer
  var ab = new ArrayBuffer(byteString.length);

  // create a view into the buffer
  var ia = new Uint8Array(ab);

  // set the bytes of the buffer to the correct values
  for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }

  // write the ArrayBuffer to a blob, and you're done
  var blob = new Blob([ab], {type: mimeString});
  return blob;

}

Note: According to MDN, BlobBuilder now is deprecated. No idea since when though. (I know this is quite an old question and answer, just keeping it a little up-to-date.) Instead the Blob accepts an array of parts as first parameter of its constructor.

The duplicated split seems a bit wasteful, given that this could be a very large string.

What is the role of the variable "ia" - its assigned a value but is never used in forming the blob. Why?

You can replace the BlobBuilder than blob. var blob = new Blob([ab], {type: mimeString}); return blob;

Note
Rectangle 27 0

javascript Blob from DataURL?


function dataURLtoBlob( dataUrl, callback )
{
    var req = new XMLHttpRequest;

    req.open( 'GET', dataUrl );
    req.responseType = 'arraybuffer'; // Can't use blob directly because of https://crbug.com/412752

    req.onload = function fileLoaded(e)
    {
        // If you require the blob to have correct mime type
        var mime = this.getResponseHeader('content-type');

        callback( new Blob([this.response], {type:mime}) );
    };

    req.send();
}

dataURLtoBlob( 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', function( blob )
{
    console.log( blob );
});

Will not work in Safari - it will throw cross-origin error if page is loaded from HTTPS.

Note
Rectangle 27 0

javascript Blob from DataURL?


FileReader.readAsArrayBuffer(Blob|File)
FileReader.readAsDataURL(Blob|File)

I have to store it as a DataURL for an indefinite period in localStorage, so using the alternative ArrayBuffer path won't work.

Note
Rectangle 27 0

javascript Blob from DataURL?


dataURItoBlob : function(dataURI, dataTYPE) {
        var binary = atob(dataURI.split(',')[1]), array = [];
        for(var i = 0; i < binary.length; i++) array.push(binary.charCodeAt(i));
        return new Blob([new Uint8Array(array)], {type: dataTYPE});
    }

The dataTYPE is embedded in dataURI and hence should be parsed as in the first answer.

input dataURI is Data URL and dataTYPE is the file type and then output blob object

Note
Rectangle 27 0

javascript Blob from DataURL?


function dataURItoBlob(dataURI) {
    if(typeof dataURI !== 'string'){
        throw new Error('Invalid argument: dataURI must be a string');
    }
    dataURI = dataURI.split(',');
    var type = dataURI[0].split(':')[1].split(';')[0],
        byteString = atob(dataURI[1]),
        byteStringLength = byteString.length,
        arrayBuffer = new ArrayBuffer(byteStringLength),
        intArray = new Uint8Array(arrayBuffer);
    for (var i = 0; i < byteStringLength; i++) {
        intArray[i] = byteString.charCodeAt(i);
    }
    return new Blob([intArray], {
        type: type
    });
}
Note
Rectangle 27 0

javascript Blob from DataURL?


function dataURItoBlob(dataURI) {
  // convert base64 to raw binary data held in a string
  // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
  var byteString = atob(dataURI.split(',')[1]);

  // separate out the mime component
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

  // write the bytes of the string to an ArrayBuffer
  var ab = new ArrayBuffer(byteString.length);
  var ia = new Uint8Array(ab);
  for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }

  // write the ArrayBuffer to a blob, and you're done
  var bb = new BlobBuilder();
  bb.append(ab);
  return bb.getBlob(mimeString);
}

Note: According to MDN, BlobBuilder now is deprecated. No idea since when though. (I know this is quite an old question and answer, just keeping it a little up-to-date.) Instead the Blob accepts an array of parts as first parameter of its constructor.

You can replace the BlobBuilder than blob. var blob = new Blob([ab], {type: mimeString}); return blob;

Note