HTML: How to limit file upload to be only images?
With HTML, how do I limit what kind of filetypes can be uploaded?
To easy the user experience, I want to limit file uploads to be only images (jpeg, gif, png).
<form method="post" action="..." enctype="multipart/form-data">
<label for="image">Photo</label>
<input name="image" type="file" />
</form>
HTML5 says <input type="file" accept="image/*">
. Of course, never trust client-side validation: Always check again on the server-side...
HTML5 File input has accept attribute and also multiple attribute. By using multiple attribute you can upload multiple images in an instance.
<input type="file" multiple accept="image/*">
You can also limit multiple mime types.
<input type="file" multiple accept="image/*,audio/*,video/*">
and another way of checking mime type using file object.
file object gives you name,size and type.
var files=e.target.files;
var mimeType=files[0].type; // You can get the mime type
You can also restrict the user for some file types to upload by the above code.
Edited
If things were as they SHOULD be, you could do this via the "Accept" attribute.
http://www.webmasterworld.com/forum21/6310.htm
However, browsers pretty much ignore this, so this is irrelavant. The short answer is, i don't think there is a way to do it in HTML. You'd have to check it server-side instead.
The following older post has some information that could help you with alternatives.
File input 'accept' attribute - is it useful?
Here is the HTML for image upload. By default it will show image files only in the browsing window because we have put accept="image/*"
. But we can still change it from the dropdown and it will show all files. So the Javascript part validates whether or not the selected file is an actual image.
<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 on the change event we first check the size of the image. And in the second if
condition we check whether or not it 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) {
// get loaded data and render thumbnail.
document.getElementById("menu_image").src = e.target.result;
$("#menu_image").show();
};
// read the image file as a data URL.
reader.readAsDataURL(this.files[0]);
};