javascript dataurl to blob

Solutions on MaxInterview for javascript dataurl to blob by the best coders in the world

showing results for - "javascript dataurl to blob"
Deb
09 Oct 2020
1function dataURItoBlob(dataURI) {
2  // convert base64 to raw binary data held in a string
3  // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
4  var byteString = atob(dataURI.split(',')[1]);
5
6  // separate out the mime component
7  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
8
9  // write the bytes of the string to an ArrayBuffer
10  var ab = new ArrayBuffer(byteString.length);
11
12  // create a view into the buffer
13  var ia = new Uint8Array(ab);
14
15  // set the bytes of the buffer to the correct values
16  for (var i = 0; i < byteString.length; i++) {
17      ia[i] = byteString.charCodeAt(i);
18  }
19
20  // write the ArrayBuffer to a blob, and you're done
21  var blob = new Blob([ab], {type: mimeString});
22  return blob;
23
24}