打开浏览器文件窗口,选择文件上传
其他需要熟悉HTMLInputElement
this.input = document.createElement("input");
this.input.setAttribute("id", "selFiles");
this.input.setAttribute("type", "file");
this.input.setAttribute("style", "display:none");//隐藏按钮
// this.input.setAttribute("accept","image/*");
this.input.setAttribute("multiple", "multiple");
document.body.appendChild(this.input);
this.input.addEventListener("input", (data) => {
const fileInput = document.getElementById("selFiles");
console.log(fileInput.files instanceof FileList); // true even if empty
let file = fileInput.files[0];
let fromData = new FormData();
fromData.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.send(formData);
});
this.input.click();//主动触发