Archive for January, 2012

HTML5: Display local images without going to server

Wednesday, January 25th, 2012

Note: assuming jquery

// get the input element
var $fileElement = $("input[type='file']");
var $image = $("img:first");

// on change
$fileElement.on("change",function(){
   if (this.files && this.files.length > 0){
       var file = this.files[0];
       var reader = new FileReader();
       reader.onload = function(e) {
	   $img.attr("src", e.target.result);
       }
       reader.readAsDataURL(file);
   }
});