input type file image only code example

Example 1: accept only image files upload html

<input type="file" name="myImage" accept="image/x-png,image/gif,image/jpeg" />

Example 2: input type="file" and display image

<p><input type="file"  accept="image/*" name="image" id="file"  onchange="loadFile(event)" style="display: none;"></p>
<p><label for="file" style="cursor: pointer;">Upload Image</label></p>
<p><img id="output" width="200" /></p>

<script>
var loadFile = function(event) {
	var image = document.getElementById('output');
	image.src = URL.createObjectURL(event.target.files[0]);
};
</script>

Example 3: only accept image file input

<input type="file" name="myImage" accept="image/*" />

Example 4: input type file select only video

accept="video/mp4,video/x-m4v,video/*"

Tags:

Misc Example