Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
164 lines (150 sloc) 7.88 KB
<!DOCTYPE html>
<!--[if lt IE 9]><html class="no-js oldie" lang="en"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8" />
<!--
Copyright (c) 2011-2012 Oroboto. All rights reserved.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
-->
<title>Base64 Drag and Drop</title>
<style type="text/css" media="screen">
body {font:14px Arial, sans-serif; margin:0 auto; width:960px;}
#drop_zone {border:2px #bbb dashed; padding:25px; text-align:center;
-moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px;}
.js #drop_zone {background:#efe;}
.no-js #drop_zone, #drop_zone.highlight {background:#fee;}
#list ol {border-bottom:1px #000 solid; margin:0 0 50px; padding:0 0 40px 20px;}
#list ol li {margin:0 0 10px;}
#list ol li:after {clear:both; content:""; display:table;} /* clearfix */
dl, dt, dd, figure {float:left; margin:0 20px 0 0;}
dl, textarea {height:75px;}
dl {width:300px;}
dt {clear:left; width:100px;}
figure {max-width:200px;}
.thumb {max-height:75px; max-width:200px;}
textarea {float:right; width:40%;}
</style>
</head>
<body>
<header>
<hgroup>
<h1>Base64 Drag and Drop</h1>
<h2>Drag and drop files in Firefox and Chrome to convert them to base64 strings.</h2>
</hgroup>
</header>
<p>If dropping images, right click on the thumbnail for the data URI.</p>
<div id="drop_zone"><noscript>JavaScript must be enabled to use the drop zone.</noscript></div>
<output id="list"></output>
<h3>Features (or, "What makes this so cool?")</h3>
<ul>
<li>encodes any file into a base64 string</li>
<li>easy drag and drop of files on your computer into the drop zone</li>
<li>works offline; files aren't uploaded anywhere, so your sensitive data is safe</li>
<li>for Firefox, no server needed, just open index.html from your file manager</li>
<li>very portable; entire tool is contained in one file (index.html)</li>
<li>previews images as data URIs, for easy copying into CSS</li>
</ul>
<h3>Requirements</h3>
<ul>
<li>Mozilla Firefox 3.6+ or Google Chrome 6+ (maybe Opera 11.1+ and IE10+ also?)</li>
<li>JavaScript <strong>ENABLED</strong> (that's where the magic happens)</li>
</ul>
<h3>Usage</h3>
<ol>
<li>Open index.html in your browser. Firefox doesn't even need it to be on a server (Chrome can only get the file info, but can't read the file to convert to base64, so either copy to your local web server or use the online version).</li>
<li>Drag files from your file manager into the drop zone.</li>
<li>???</li>
<li>Profit!</li>
</ol>
<footer>
<h3>License</h3>
<a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a>
<p class="copyright">&copy; 2011-2012 <a href="http://oroboto.com/">Oroboto</a>. All rights reserved.</p>
</footer>
<a href="https://github.com/oroboto/base64-drag-and-drop"><img style="position:absolute; top:0; right:0; border:0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub" /></a>
<script>
(function(){
var processFiles = function (event) {
event.stopPropagation();
event.preventDefault();
removeDropZoneClass();
// FileList object of File objects
var files = event.dataTransfer.files;
var output = [];
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
// closure to capture file info
reader.onload = (function(file, index) {
return function(e) {
var dataUri = e.target.result,
base64 = dataUri.substr(dataUri.indexOf(',') + 1),
textarea, image, figcaption;
textarea = ['<textarea onfocus="this.select()">', base64, '</textarea>'].join('');
if (file.type.match('image.*')) {
if (dataUri.length > 32768) {
figcaption = '<figcaption>In IE8, data URIs cannot be larger than 32,768 characters.</figcaption>';
}
image = ['<figure><img class="thumb" src="', dataUri, '" title="', file.name, '"/>',
figcaption, '</figure>'].join('');
}
document.getElementById(['file_', index + 1].join('')).innerHTML += [image, textarea].join('');
};
})(f, i);
// read file as data URI
reader.readAsDataURL(f);
var data = ['<li id="file_', i + 1, '"><h3>', f.name, '</h3><dl>',
'<dt>Type</dt><dd>', f.type || 'n/a', '</dd>',
'<dt>Size</dt><dd>', f.size, ' bytes</dd>',
'<dt>Last Modified</dt><dd>', f.lastModifiedDate ? f.lastModifiedDate.toDateString() : 'n/a', '</dd>',
'</dl></li>'].join('');
output.push(data);
}
if (files.length) {
document.getElementById('list').innerHTML = '<ol>' + output.join('') + '</ol>';
}
};
var highlightDropZone = function (e) {
e.stopPropagation();
e.preventDefault();
document.getElementById('drop_zone').setAttribute('class', 'highlight');
}
var removeDropZoneClass = function () {
document.getElementById('drop_zone').removeAttribute('class');
}
// add event listeners if File API is supported
var dropZone = document.getElementById('drop_zone');
if (window.File && window.FileReader && window.FileList && window.Blob) {
dropZone.addEventListener('drop', processFiles, false);
dropZone.addEventListener('dragover', highlightDropZone, false);
dropZone.addEventListener('dragenter', highlightDropZone, false);
dropZone.addEventListener('dragleave', removeDropZoneClass, false);
dropZone.innerHTML = 'Drop files here';
} else {
dropZone.innerHTML = 'The File APIs are not fully supported in this browser.';
dropZone.className = 'highlight';
}
// change doc element class to show JS support
var docEl = document.documentElement;
docEl.className = docEl.className.replace('no-', '');
})();
</script>
</body>
</html>