Here is the HTML for uploading the image. By default, it will only show image files in the viewport, because we put accept="image/*" . But we can still change it from the drop-down list, and it will display all the files. Therefore, the Javascript part checks to see if the selected file is actual.
<div class="col-sm-8 img-upload-section"> <input name="image3" type="file" accept="image/*" id="menu_images"/> <img id="menu_image" class="preview_img" /> <input type="submit" value="Submit" /> </div>
Here, in case of a change, we first check the image size. And in the second if state, we check if this is an image file.
this.files[0].type.indexOf("image") will be -1 if it is not an image file.
document.getElementById("menu_images").onchange = function () { var reader = new FileReader(); if(this.files[0].size>528385){ alert("Image Size should not be greater than 500Kb"); $("#menu_image").attr("src","blank"); $("#menu_image").hide(); $('#menu_images').wrap('<form>').closest('form').get(0).reset(); $('#menu_images').unwrap(); return false; } if(this.files[0].type.indexOf("image")==-1){ alert("Invalid Type"); $("#menu_image").attr("src","blank"); $("#menu_image").hide(); $('#menu_images').wrap('<form>').closest('form').get(0).reset(); $('#menu_images').unwrap(); return false; } reader.onload = function (e) {
Vineesh Puttanisseri Jun 28 '16 at 14:21 2016-06-28 14:21
source share