Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear,
i have to browse only .xls or .xlsx file to import data to database in mvc application so i need to allow user to select only xls or xlsx. my code is below but its not working

<input type="file" name="ImportExcel" title="Select File" id="importData" accept=".xls,.xlsx" />

please anyone help me
Posted
Updated 6-Apr-21 8:06am

try below

HTML
<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"  />  


Or you could use javascript to check the file type

JavaScript
<script type="text/javascript" language="javascript">
function checkfile(sender) {
    var validExts = new Array(".xlsx", ".xls");
    var fileExt = sender.value;
    fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
    if (validExts.indexOf(fileExt) < 0) {
      alert("Invalid file selected, valid files are of " +
               validExts.toString() + " types.");
      return false;
    }
    else return true;
}
</script>

<input type="file" id="file"  önchange="checkfile(this);" />
 
Share this answer
 
v2
<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />

this is not working on chrome browser...
 
Share this answer
 
v3
Comments
Patrice T 17-Aug-18 2:23am    
What say Chrome support service ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900