10 examples of 'base64 to blob javascript' in JavaScript

Every line of 'base64 to blob javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
40function blobToBase64(blob, cb) {
41 var reader = new FileReader();
42
43 reader.onload = function() {
44 var dataUrl = reader.result;
45 var base64 = dataUrl.split(',')[1];
46 cb(base64);
47 };
48
49 reader.readAsDataURL(blob);
50}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

611export function base64ToBlob(base64: string): Blob {
612 const sliceSize = 1024
613 const byteChars = globalThis.atob(base64)
614 const byteArrays = []
615 let len = byteChars.length
616
617 for (let offset = 0; offset < len; offset += sliceSize) {
618 const slice = byteChars.slice(offset, offset + sliceSize)
619
620 const byteNumbers = new Array(slice.length)
621 for (let i = 0; i < slice.length; i++) {
622 byteNumbers[i] = slice.charCodeAt(i)
623 }
624
625 const byteArray = new Uint8Array(byteNumbers)
626
627 byteArrays.push(byteArray)
628 len = byteChars.length
629 }
630
631 return new Blob(byteArrays, { type: 'image/jpeg' })
632}
23function base64ToBlob_(base64, mimetype) {
24 var BASE64_MARKER = ';base64,';
25 var raw = window.atob(base64);
26 var rawLength = raw.length;
27 var uInt8Array = new Uint8Array(rawLength);
28 for (var i = 0; i < rawLength; ++i) {
29 uInt8Array[i] = raw.charCodeAt(i);
30 }
31
32 if (imagelib.util.hasBlobConstructor()) {
33 return new Blob([uInt8Array], {type: mimetype})
34 }
35
36 var bb = new BlobBuilder();
37 bb.append(uInt8Array.buffer);
38 return bb.getBlob(mimetype);
39}
4function blobToBase64(blobOrBuffer, callback) {
5 blobOrBufferToBinaryString(blobOrBuffer, function (base64) {
6 callback(btoa(base64));
7 });
8}
93decodeBlob(blob) {
94 return this.getClass().decodeBlob(blob);
95}
23function base64ToBlob(data) {
24 var mimeString = '';
25 var raw, uInt8Array, i, rawLength;
26
27 raw = data.replace(rImageType, function(header, imageType) {
28 mimeString = imageType;
29
30 return '';
31 });
32
33 raw = atob(raw);
34 rawLength = raw.length;
35 uInt8Array = new Uint8Array(rawLength); // eslint-disable-line
36
37 for (i = 0; i < rawLength; i += 1) {
38 uInt8Array[i] = raw.charCodeAt(i);
39 }
40
41 return new Blob([uInt8Array], {type: mimeString});
42}
51function base64DataUriToBlob(url) {
52 // 5 is length of "data:" prefix
53 var mime = url.substr(5, url.indexOf(';') - 5);
54 var data = atob(url.substr(url.indexOf(',') + 1));
55 /* global Uint8Array */
56 var binary = new Uint8Array(data.length);
57
58 for (var i = 0; i < data.length; i++) {
59 binary[i] = data[i].charCodeAt(0);
60 }
61
62 try {
63 return new Blob([binary], { type: mime });
64 } catch (e) {
65 return null;
66 }
67}
3function createBlobFromeDataURL(base64DataURL) {
4 var strippedData = base64DataURL.substr(
5 base64DataURL.indexOf(';base64,') + ';base64,'.length);
6 strippedData = atob(strippedData);
7 var rawData = new Uint8Array(strippedData.length);
8 for (var i = 0; i < strippedData.length; i++) {
9 rawData[i] = strippedData.charCodeAt(i);
10 }
11 return new Blob([rawData.buffer], {type: 'image/png'});
12}
36export function b64toBlob(dataURI) {
37 // convert base64 to raw binary data held in a string
38 // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
39 const byteString = atob(dataURI.split(',')[1]);
40
41 // separate out the mime component
42 const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
43
44 // write the bytes of the string to an ArrayBuffer
45 const ab = new ArrayBuffer(byteString.length);
46 const ia = new Uint8Array(ab);
47 for (let i = 0; i < byteString.length; i++) {
48 ia[i] = byteString.charCodeAt(i);
49 }
50
51 // write the ArrayBuffer to a blob, and you're done
52 return new Blob([ab]);
53}
173function blobToBase64String(blob) {
174 return blobToBinaryString(blob).then(function (binary) {
175 return btoa(binary);
176 });
177}

Related snippets