Điều tốt nhất bạn có thể làm (natively) là để kiểm tra tập tin được lựa chọn:
<input id="uploadFile" type="file" onchange="FileSelected(this)" />
Script:
function FileSelected(sender)
{
if (check(sender.value)) //check is you function to check extension
{...}
else
{...}
}
Mẫu mã: (kiểm tra chỉ jpg)
http://jsbin.com/sibose/2/edit
Chỉnh sửa
bằng chrome. IE10 bạn có thể làm:
<!-- (IE 10+, Chrome) -->
<input type="file" accept=".xls,.xlsx">
Với FF:
<!-- (IE 10+, Chrome, Firefox) -->
<input type="file"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel" />
bản demo: http://jsbin.com/jihoku/2/edit
Nguồn
2014-09-16 07:21:02
Tôi không nghĩ rằng điều này có thể được thực hiện. Chắc chắn, không đáng tin cậy, vì nó sẽ phụ thuộc vào cách trình duyệt đã chọn để thực hiện chức năng cụ thể này. – ne1410s