使用 input 标签属性:(多种文件用 , 分隔)
<input type="file" id="file"
accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
使用 js 限定,使用字符串的 endWith 方法即可
let files = this.files[0]; //获取file组件中的文件
let name = files.name; //文件名
let size = files.size; //文件大小
if (s.endsWith(".txt")) { // 合法
document.write(1);
} else {
document.write(2); // 不合法
}
let formData = new FormData(); // 发送ajax请求
formData.append('file', files);
$.ajax({
type: "POST",
url: "?action=upload",
data: formData,
contentType: false,
processData: false,
success: function(r) {
r = $.parseJSON(r)
}
})