vue项目本地文件上传功能..
结构:
<button @click="uploadingBtn()"> //上传文件 <input type="file" @change="fileChange($event)" class="change" id="file1" ref="fileRef" hidden/> </button>
按钮
methods:{ uploadingBtn(){ this.$refs.fileRef.dispatchEvent(new MouseEvent("click")); },
fileChange(event) {
let fileName = document.getElementById("fileName")
try {
const fu = event.target;
if (fu == null) return;
let myFile = fu.files[0].name; //文件名
let ext = myFile.split(".").pop(); //文件后缀
//判断文件类型
if (ext == "pdf" || ext == "docx" || ext == "doc") {
//写入业务逻辑
}
//文件类型不正确
else {}
} catch (error) {}
},
}