The input file is only the .xls file

I am working on html and java script. I have file type input so that I want the input file to be only a .xls file

<input type='file' id ="browse" name ="browse"/> 

Now I want the selected files to be only the .xls .. file and read that xls file. how to do it with java script ... And if someone can explain me JavaScript code that would be more appreciated.

+6
source share
3 answers

This will check the file extension after selecting the file. If it is not .xls , then it gives a warning:

 document.getElementById("browse").onchange = function() { var fileName = this.value; var fileExtension = fileName.substr(fileName.length - 4); console.log(fileExtension); if (fileExtension != ".xls") { alert("That ain't no .xls file!"); } } 

Demo: http://jsfiddle.net/sn5sY/

+3
source

Try with this code.

 <input type='file' id ="browse" name ="browse"/> var file = document.getElementById('browse'); file.onchange = function(e){ var ext = this.value.match(/\.([^\.]+)$/)[1]; switch(ext) { case 'xls': alert('allowed'); break; default: alert('not allowed'); this.value=''; } }; 

And demo

+1
source

This is not possible with standard HTML 4

Possible duplicate Limit file format when using <input type = "file" gt ;?

HTML 5 has an accept attribute, but it is not guaranteed. More information can be found in the HTML 5 specification http://www.w3.org/html/wg/drafts/html/master/forms.html#attr-input-accept

-1
source

Source: https://habr.com/ru/post/947744/


All Articles