方法一(适合传文件且带参数的方法)
HTML内容 <a href="javascript:;" class="select-file"> <input type="file" @change="fileChange"> 选择文件 </a> JS内容 fileChange (e) { let file = e.target.files[0] this.newFile = file console.log(file) let formData = new FormData(); formData.append("uploadFile", this.newFile); formData.append("remark", this.remark); // 其它参数 formData.append("name", this.name); // 其它参数
let data = await baseManage.checkStation(formData)
},
方法二(适合只传文件不带参数的方法)
HTML内容 <a href="javascript:;" class="select-file"> <input type="file" @change="fileChange"> 选择文件 </a> JS内容 fileChange (e) { let file = e.target.files[0] this.fileName = `${file.name.slice(0, file.name.lastIndexOf('.'))}_${new Date().getTime()}.xlsx` this.newFile = new File([file], this.fileName, { type: file.type }); await axios({ method: "post", url:'http:xxxxx', data: this.newFile, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then((res) => { if (res.code === 0) { this.$message.success("上传完成") } else { this.$message.warning(res.msg) } }).finally(()=> this.state = '成功') },