var image = new Image();
var file;
function readImage(file) { // function for image preview before upload
var useBlob = false && window.URL;
var reader = new FileReader();
reader.addEventListener("load", function () {
image.addEventListener("load", function () {
var preview = $(".preview");
preview.html(this);
if (useBlob) window.URL.revokeObjectURL(image.src);
});
image.style = "margin-left:auto;margin-right: auto;display:block;;max-height:400px;max-width:570px;";
image.src = useBlob ? window.URL.createObjectURL(file) : reader.result;
image.id = "upload_image_view"; // add image preview on image select
});
reader.readAsDataURL(file);
$('.upload').removeAttr('disabled');
}
// Call image validation on file uploader change event
$(document).on("change", ".file", function () {
var input = $('.file');
file = this.files[0];
if (!file) {
window.alert"File upload not supported by your browser.");
} else {
fileName = '';
fileName = file.name.replace(/\\/g, '/').replace(/.*\//, '');
// JPE|PNG|JPEG|JPG|GIF|BMP allowed
if (/\.(jpe?g|png|gif|bmp)$/i.test(file.name)) {
if ((Math.round(file.size / 1024 / 1024)) <= 1) {
$('.form-control').val(fileName);
readImage(file);
} else {
window.alert('Maximum (1 MB) of image size allowed.');
}
}
else {
window.alert('Only image file allowed.');
}
}
});
Comments
Post a Comment