JS/VUE按钮点击上传文件
直接上代码
importFile() { const fileType = ['jpg', 'png', 'gif'] const inputFile = document.createElement('input') inputFile.type = 'file' inputFile.style.display = 'none' document.body.appendChild(inputFile) inputFile.click() inputFile.addEventListener('change', () => { const file = inputFile.files[0] var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1) if (!fileType.includes(testmsg)) { this.$message.warning('上传的文件格式只能是,jpg,png,gif') document.body.removeChild(inputFile) return false } let fd = new FormData() fd.append('file', file) //上传接口 upapi.upload(fd).then((res) => { if (res.code == 10000) { this.formData.icon = res.data this.$message.success(res.message) } else { this.$message.error(res.message) } }) }) },