We can preview the uploaded image easily with jQuery or JavaScript before actual upload of file.
Just Follow these Steps
HTML Code
<form id="Imgfrm">
<input id="imgfile" type="file" />
<img id="preview" src="#" alt="your image" /><
/form>
Then use following jQuery Code (You can use pure JavaScript also)
$("#imgfile").change(function(){
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').attr('src', e.target.result);
}
reader.readAsDataURL(this.files[0]);
}
});
With pure javascript
<form id="Imgfrm">
<input id="imgfile" type="file" onchange="readURL(this)"; />
<img id="preview" src="#" alt="your image" /><
/form>
Then use following JS Code
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById('preview').src=e.target.result;
}
reader.readAsDataURL(input.files[0]);
}
}
Reblogged this on Snippetz.
Reblogged this on ITechonology.
Its so great! It is really good. But it does not work with Safari. Do you have any suggestion?
I think you use old version of safari. Please update your safari version. File reader is supporting from safari version 6.1.
Thanks
How to make it back with no image when user decide to cancel or not choose an image?
Provide a button to remove image. On its click event write javascript to remove src of image tag.
Thanks & Regards,
Mohammed Safeer
Its remove only source of an image but selected image is there for submit the form. How can completely remove it?
When I use it in first time its working correctly. But now it is not working what can I do?
maacihhhh…,,