Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit; things work

  • Loading branch information...
commit 5f273c7a5027f676e5cbbac49910b5785d982e56 0 parents
@beatgammit authored
21 README.md
@@ -0,0 +1,21 @@
+Intro
+=====
+
+toDataURL takes whatever you give it (assuming it's supported) and turns it into something you can download.
+
+Supported inputs are:
+
+* string- uses built-in btoa function
+* Array- takes Array of numbers and assumes they're bytes (converts to Uint8Array internally)
+* Uint8Array- uses a home-grown base64 conversion function
+
+Exported Methods
+----------------
+
+* toDataURL- takes up to three arguments
+ * data- one of the supported formats listed above
+ * mimeType- optional; application/octet-stream is the default
+ * download- optional; defaults to false; this will automagically download the data if true
+* openDataURL- takes up to two arguments
+ * data- the dataURL output from toDataURL
+ * name- name of the window to open it in (defaults to _self)
8 configure
@@ -0,0 +1,8 @@
+#/bin/sh
+
+if [ ! -d "js" ]
+then
+ mkdir js
+fi
+cd js
+ender build jeesh toDataURL
55 index.html
@@ -0,0 +1,55 @@
+<html>
+<head>
+<title>Download Stuff</title>
+<script src='js/ender.js'></script>
+<script>
+(function () {
+ var $ = require('ender');
+
+ $.domReady(function () {
+ $('button').bind('click', function () {
+ var a = $.toDataURL($('textarea').val()),
+ uint8, i;
+
+ console.log('As string:');
+ console.log('DataURL:', a);
+ console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
+ console.log('');
+
+ a = Array.prototype.slice.call($('textarea').val()).map(function (item) {
+ return item.charCodeAt(0);
+ });
+ a = $.toDataURL(a);
+ console.log('As byte array:');
+ console.log('DataURL:', a);
+ console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
+ console.log('');
+
+ a = $('textarea').val();
+ uint8 = new Uint8Array(a.length);
+ for (i = uint8.length; i >= 0; i -= 1) {
+ uint8[i] = a.charCodeAt(i);
+ }
+ a = $.toDataURL(uint8);
+ console.log('As Uint8Array:');
+ console.log('DataURL:', a);
+ console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
+
+ if (confirm ('Would you like to download your text?')) {
+ $.openDataURL(a);
+ }
+ });
+ });
+}());
+</script>
+</head>
+<body>
+
+<textarea>
+Here is some text. Woohoo!
+</textarea>
+
+<button>Download</button>
+
+</body>
+</html>
13 package.json
@@ -0,0 +1,13 @@
+{
+ "author": "T. Jameson Little <t.jameson.little@gmail.com>",
+ "name": "toDataURL",
+ "description": "Download something in the browser as a file",
+ "keywords": ["ender", "dataURL", "download"],
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/beatgammit/toDataURL.git"
+ },
+ "dependencies": {},
+ "main": "src/download.js"
+}
90 src/download.js
@@ -0,0 +1,90 @@
+(function (exports) {
+ 'use strict';
+
+ var lookup = [
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/'
+ ];
+
+ function uint8ToBase64(uint8) {
+ var i,
+ extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
+ output = "",
+ temp, length;
+
+ function tripletToBase64 (num) {
+ return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F];
+ };
+
+ // go through the array every three bytes, we'll deal with trailing stuff later
+ for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
+ temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
+ output += tripletToBase64(temp);
+ }
+
+ // this prevents an ERR_INVALID_URL in Chrome (Firefox okay)
+ switch (output.length % 4) {
+ case 1:
+ output += '=';
+ break;
+ case 2:
+ output += '==';
+ break;
+ default:
+ break;
+ }
+
+ return output;
+ }
+
+ function openDataURL(dataURI, name) {
+ window.open(dataURI, name || '_self');
+ }
+
+ function createDataURL (data, mimeType, autoDownload) {
+ var tmp, base64, dataURI, i;
+
+ if (!data || (typeof data !== 'string' && !(data instanceof Array) && !(data instanceof Uint8Array))) {
+ throw 'Data must be a non-empty string, Array, or Uint8Array';
+ }
+
+ if (typeof mimeType !== 'string') {
+ autoDownload = mimeType;
+ mimeType = 'application/octet-stream';
+ }
+ if (typeof autoDownload !== 'boolean') {
+ autoDownload = false;
+ }
+
+ if (typeof data === 'string') {
+ tmp = encodeURIComponent(data);
+ tmp = unescape(tmp || data.toString());
+ base64 = btoa(tmp);
+ } else if (data instanceof Array) {
+ tmp = new Uint8Array(data.length);
+ for (i = data.length; i >= 0; i -= 1) {
+ tmp[i] = data[i] & 0xFF;
+ }
+ base64 = uint8ToBase64(tmp);
+ } else if (data instanceof Uint8Array) {
+ base64 = uint8ToBase64(data);
+ }
+
+ dataURI = 'data:' + mimeType + ';base64,' + base64;
+
+ if (autoDownload) {
+ openDataURL(dataURI);
+ }
+
+ return dataURI;
+ }
+
+ exports.toDataURL = createDataURL;
+ exports.openDataURL = openDataURL;
+}(typeof module !== 'undefined' ? module.exports : window));
Please sign in to comment.
Something went wrong with that request. Please try again.