Html File Input — "accept" Attribute Not Working
After reading through the comments on this post, I came up with the following syntax for the accept attribute: Images accept attribute as shown in my question, because it will work on desktop.
const supportedExtensions = ['jpeg', 'jpg', 'png', 'gif'];
consthandleChange = ({ target }) => {
const path = target.value.split('.');
const extension = `${path[path.length - 1]}`;
if (supportedExtensions.includes(extension)) {
// TODO: upload
} else {
// TODO: show "invalid file type" message to user// reset value
target.value = '';
}
}
Solution 2:
The detail listing of browser support for "accept" attribute is listed in w3 schools. Have a look. It may help you.
Solution 3:
I got the same problem, found this page, here is my workaround using onChange
event.
I know this isn't true filtering and this is pretty ugly (I don't it), but it works indeed. I tested on my iOS and Android devices.
<scripttype="text/javascript">let file;
functioncheckFile() {
file = document.querySelector('input[type=file]').files[0];
if (file.type != "image/png") {
file = null;
document.getElementById('image').remove();
let div = document.getElementById('div');
let image = document.createElement('input');
image.setAttribute('type', 'file');
image.setAttribute('id', 'image');
image.setAttribute('accept', 'image/png');
image.setAttribute('onchange', 'checkFile()');
div.appendChild(image);
window.alert('unsupported file type');
}
}
</script><divid="div"><inputtype="file"id="image"accept="image/png"onchange="checkFile()"></div>
Post a Comment for "Html File Input — "accept" Attribute Not Working"