1.accept属性
<input type="file" id="f1" name="file" accept=".xls,.xlsx">
2.通过js来限制
$("#submit").click(function () {
var formData = new FormData();
formData.append("csrfmiddlewaretoken", $("[name='csrfmiddlewaretoken']").val());
formData.append("f1", $("#f1")[0].files[0]);
if (formData.getAll('f1') == "undefined") {
alert("请先选择需要上传的文件后再点击提交按钮!")
}
var name = document.getElementById('f1').value;
var fileName = name.substring(name.lastIndexOf(".") + 1).toLowerCase();
if (fileName != "xls" && fileName != "xlsx") {
alert("请选择execl格式文件上传!");
return
}