Skip to content

Instantly share code, notes, and snippets.

@stugoo
Last active December 12, 2015 05:48
Show Gist options
  • Save stugoo/4724127 to your computer and use it in GitHub Desktop.
Save stugoo/4724127 to your computer and use it in GitHub Desktop.
File upload Preview // single file
fileUploadPreview = function () {
var self = this,
uploader = document.getElementById("files"),
output = document.getElementById('preview_image'),
initialise = function() {
uploader.addEventListener('change', handleFileSelect, false);
},
handleFileSelect = function(e) {
var files = e.target.files,
f= files[0]; // FileList object
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
output.innerHTML = "";
output.insertBefore(span, null);
};
})(f);
reader.readAsDataURL(f);
};
if (window.File && window.FileReader && window.FileList && window.Blob) {
initialise();
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment