Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 20.23: Add dataURLToBlob helper
- Loading branch information
Kamil Kisiela
authored and
Dotan Simha
committed
Nov 22, 2016
1 parent
7f5c13d
commit bcd4800
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Converts DataURL to Blob object | ||
* | ||
* https://github.com/ebidel/filer.js/blob/master/src/filer.js#L137 | ||
* | ||
* @param {String} dataURL | ||
* @return {Blob} | ||
*/ | ||
export function dataURLToBlob(dataURL) { | ||
const BASE64_MARKER = ';base64,'; | ||
|
||
if (dataURL.indexOf(BASE64_MARKER) === -1) { | ||
const parts = dataURL.split(','); | ||
const contentType = parts[0].split(':')[1]; | ||
const raw = decodeURIComponent(parts[1]); | ||
|
||
return new Blob([raw], {type: contentType}); | ||
} | ||
|
||
const parts = dataURL.split(BASE64_MARKER); | ||
const contentType = parts[0].split(':')[1]; | ||
const raw = window.atob(parts[1]); | ||
const rawLength = raw.length; | ||
const uInt8Array = new Uint8Array(rawLength); | ||
|
||
for (let i = 0; i < rawLength; ++i) { | ||
uInt8Array[i] = raw.charCodeAt(i); | ||
} | ||
|
||
return new Blob([uInt8Array], {type: contentType}); | ||
} |