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>
Now in the html part
this code will check if the uploaded file is a jpg file or not and restricts the upload of other types
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.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.