javascript - jQuery how to check if uploaded file is an image without checking extensions? -
newbie here. problem have written method checks uploaded file size , extension in order validate it. however, checking extensions not solution kind of validation may cause lot of problems. want check actual file type , validate without using extension method. have tried use jquery file validator no avail... snippet current code:
<input type='file' id='imageloader' name='imageloader' accept="image/*" data-type='image' />
script:
app.dispatcher.on("uploadpic", function() { $(":file").change(function() { if (this.files && this.files[0] && this.files[0].name.match(/\.(jpg|jpeg|png|gif)$/) ) { if(this.files[0].size>1048576) { alert('file size larger 1mb!'); } else { var reader = new filereader(); reader.onload = imageisloaded; reader.readasdataurl(this.files[0]); } } else alert('this not image file!'); }); function imageisloaded(e) { result = e.target.result; $('#image').attr('src', result); }; });
it called once upload input changes , after validation uploads , displays image. now, care validation , or ideas appreciated!
try this:
var file = this.files[0]; var filetype = file["type"]; var validimagetypes = ["image/gif", "image/jpeg", "image/png"]; if ($.inarray(filetype, validimagetypes) < 0) { // invalid file type code goes here. }
Comments
Post a Comment