├── .gitignore
├── README.md
├── b64toBlob.js
├── examples
└── example.html
├── package.json
├── types
├── index.d.ts
├── test.ts
├── tsconfig.json
└── tslint.json
└── yarn.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | `b64toBlob(b64Data: string, contentType?: string): Blob` converts a base64
2 | string to a Blob object [as described in this Stack Overflow post](https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript).
3 |
4 | Note that the `Blob` class only exists in browsers, not Node, so this package
5 | is only meant for use in the browser and simulated browser environments, not as
6 | part of a typical Node server.
7 |
8 | ## Example Usage
9 |
10 | This module uses the UMD returnExports pattern to export itself for either AMD
11 | or Node/Webpack module loading, falling back to a global browser definition if
12 | neither are available.
13 |
14 | ### With Webpack
15 |
16 |
17 |
18 | npm install b64-to-blob
19 |
20 |
21 |
22 | var b64toBlob = require('b64-to-blob');
23 |
24 | var contentType = 'image/png';
25 | var b64Data =
26 | 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACN' +
27 | 'byblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHx' +
28 | 'gljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
29 |
30 | var blob = b64toBlob(b64Data, contentType);
31 |
32 | var blobUrl = URL.createObjectURL(blob);
33 | window.location = blobUrl;
34 |
35 | ### Without a Build Step
36 |
37 |
38 |
39 |
40 |
52 |
--------------------------------------------------------------------------------
/b64toBlob.js:
--------------------------------------------------------------------------------
1 | (function(root, globalName, factory) {
2 | if (typeof define === 'function' && define.amd) {
3 | // AMD:
4 | define([], factory);
5 | } else if (typeof module === 'object' && module.exports) {
6 | // Node:
7 | module.exports = factory();
8 | // Use module export as simulated ES6 default export:
9 | module.exports.default = module.exports;
10 | } else {
11 | // Browser:
12 | window[globalName] = factory();
13 | }
14 | }(this, 'b64toBlob', function() {
15 | 'use strict';
16 |
17 | return function b64toBlob(b64Data, contentType, sliceSize) {
18 | contentType = contentType || '';
19 | sliceSize = sliceSize || 512;
20 |
21 | var byteCharacters = atob(b64Data);
22 | var byteArrays = [];
23 |
24 | for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
25 | var slice = byteCharacters.slice(offset, offset + sliceSize);
26 |
27 | var byteNumbers = new Array(slice.length);
28 | for (var i = 0; i < slice.length; i++) {
29 | byteNumbers[i] = slice.charCodeAt(i);
30 | }
31 |
32 | var byteArray = new Uint8Array(byteNumbers);
33 |
34 | byteArrays.push(byteArray);
35 | }
36 |
37 | var blob = new Blob(byteArrays, {type: contentType});
38 | return blob;
39 | };
40 | }));
41 |
--------------------------------------------------------------------------------
/examples/example.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
24 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "b64-to-blob",
3 | "version": "1.2.19",
4 | "homepage": "https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript",
5 | "repository": "jeremybanks/b64-to-blob",
6 | "author": "Jeremy Banks <_@jeremy.ca> (https://jeremy.ca)",
7 | "license": "CC0-1.0",
8 | "main": "./b64toBlob.js",
9 | "types": "./types/index.d.ts",
10 | "scripts": {
11 | "test": "dtslint types",
12 | "preversion": "npm test",
13 | "prepublishOnly": "npm test"
14 | },
15 | "devDependencies": {
16 | "dtslint": "^0.1.2",
17 | "typescript": "^2.4.2"
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/types/index.d.ts:
--------------------------------------------------------------------------------
1 | // TypeScript Version: 2.1
2 |
3 | declare module 'b64-to-blob' {
4 | function b64toBlob(b64Data: string, contentType?: string): Blob;
5 | export default b64toBlob;
6 | }
7 |
--------------------------------------------------------------------------------
/types/test.ts:
--------------------------------------------------------------------------------
1 | import b64ToBlob from 'b64-to-blob';
2 |
3 | // $ExpectType Blob
4 | b64ToBlob('');
5 |
6 | // $ExpectType Blob
7 | b64ToBlob('aGVsbG8gd29ybGQ=');
8 |
9 | // $ExpectType Blob
10 | b64ToBlob('', 'text/plain');
11 |
12 | // $ExpectError
13 | b64ToBlob();
14 |
15 | // $ExpectError
16 | b64ToBlob('', true);
17 |
18 | // $ExpectError
19 | b64ToBlob(new Blob());
20 |
21 | // $ExpectError
22 | b64ToBlob('', 'text/plain', 'text/plain');
23 |
--------------------------------------------------------------------------------
/types/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "lib": [
5 | "dom",
6 | "es5"
7 | ],
8 | "noImplicitAny": true,
9 | "noImplicitThis": true,
10 | "strictNullChecks": true,
11 | "baseUrl": ".",
12 | "paths": { "b64-to-blob": ["."] },
13 | "noEmit": true
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/types/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "dtslint/dtslint.json",
3 | "rules": {
4 | "no-single-declare-module": false
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/yarn.lock:
--------------------------------------------------------------------------------
1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2 | # yarn lockfile v1
3 |
4 |
5 | any-promise@^1.0.0, any-promise@^1.3.0:
6 | version "1.3.0"
7 | resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
8 |
9 | dtslint@^0.1.2:
10 | version "0.1.2"
11 | resolved "https://registry.yarnpkg.com/dtslint/-/dtslint-0.1.2.tgz#574beb72633f452689605de1806da6281452ac2e"
12 | dependencies:
13 | fs-promise "^2.0.0"
14 | parsimmon "^1.2.0"
15 | strip-json-comments "^2.0.1"
16 | tsutils "^1.1.0"
17 |
18 | fs-extra@^2.0.0:
19 | version "2.1.2"
20 | resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-2.1.2.tgz#046c70163cef9aad46b0e4a7fa467fb22d71de35"
21 | dependencies:
22 | graceful-fs "^4.1.2"
23 | jsonfile "^2.1.0"
24 |
25 | fs-promise@^2.0.0:
26 | version "2.0.3"
27 | resolved "https://registry.yarnpkg.com/fs-promise/-/fs-promise-2.0.3.tgz#f64e4f854bcf689aa8bddcba268916db3db46854"
28 | dependencies:
29 | any-promise "^1.3.0"
30 | fs-extra "^2.0.0"
31 | mz "^2.6.0"
32 | thenify-all "^1.6.0"
33 |
34 | graceful-fs@^4.1.2, graceful-fs@^4.1.6:
35 | version "4.1.11"
36 | resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
37 |
38 | jsonfile@^2.1.0:
39 | version "2.4.0"
40 | resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
41 | optionalDependencies:
42 | graceful-fs "^4.1.6"
43 |
44 | mz@^2.6.0:
45 | version "2.6.0"
46 | resolved "https://registry.yarnpkg.com/mz/-/mz-2.6.0.tgz#c8b8521d958df0a4f2768025db69c719ee4ef1ce"
47 | dependencies:
48 | any-promise "^1.0.0"
49 | object-assign "^4.0.1"
50 | thenify-all "^1.0.0"
51 |
52 | object-assign@^4.0.1:
53 | version "4.1.1"
54 | resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
55 |
56 | parsimmon@^1.2.0:
57 | version "1.6.2"
58 | resolved "https://registry.yarnpkg.com/parsimmon/-/parsimmon-1.6.2.tgz#28fbe6b8caa38ab89f52b77f8c7743563c7d9aff"
59 |
60 | strip-json-comments@^2.0.1:
61 | version "2.0.1"
62 | resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
63 |
64 | thenify-all@^1.0.0, thenify-all@^1.6.0:
65 | version "1.6.0"
66 | resolved "https://registry.yarnpkg.com/thenify-all/-/thenify-all-1.6.0.tgz#1a1918d402d8fc3f98fbf234db0bcc8cc10e9726"
67 | dependencies:
68 | thenify ">= 3.1.0 < 4"
69 |
70 | "thenify@>= 3.1.0 < 4":
71 | version "3.3.0"
72 | resolved "https://registry.yarnpkg.com/thenify/-/thenify-3.3.0.tgz#e69e38a1babe969b0108207978b9f62b88604839"
73 | dependencies:
74 | any-promise "^1.0.0"
75 |
76 | tsutils@^1.1.0:
77 | version "1.9.1"
78 | resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-1.9.1.tgz#b9f9ab44e55af9681831d5f28d0aeeaf5c750cb0"
79 |
80 | typescript@^2.4.2:
81 | version "2.4.2"
82 | resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.4.2.tgz#f8395f85d459276067c988aa41837a8f82870844"
83 |
--------------------------------------------------------------------------------