<input type="file" id="file" οnchange="checkFileExtension(this);" />
function checkFileExtension(file) {
var validExtensions = new Array(".xlsx", ".xls");
var fileExtension = file.value;
fileExtension = fileExtension.substring(fileExtension.lastIndexOf('.'));
if (validExtensions.indexOf(fileExtension) < 0) {
// Alert massage to user.
alert("Only " + validExtensions.toString() + " types are allowed.");
// Clean file info.
file.value = "";
return false;
} else {
return true;
}
}
获取 File对象
var file = document.getElementById('file').files[0];