Skip to content

Instantly share code, notes, and snippets.

@ardianzzz
Created September 10, 2012 16:36
Show Gist options
  • Save ardianzzz/3691990 to your computer and use it in GitHub Desktop.
Save ardianzzz/3691990 to your computer and use it in GitHub Desktop.
Input file
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('image-link-preview').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
var files = evt.target.files; // FileList object
// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<b>', escape(f.name), '</b> (', f.type || 'n/a', ') - ',
f.size,'');
}
document.getElementById('image-link-preview').innerHTML = '<div>' + output.join('') + '</div>';
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment