上传文件,可以 new FormData() 来上传FormData格式的数据
//new一个对象
formdata = new FormData();
//.append方法为formdata增加属性
this.formdata.append("Name", this.form.Name);
this.formdata.append("Version",this.form.Version);
this.formdata.append("Note", this.form.Note);
this.formdata.append("FileName",this.form.FileName);
//文件,fileList[0].raw为elupload的属性,:file-list="fileList",查一下就行
this.formdata.append("file", this.fileList[0].raw);
//.set方法为formdata修改属性
this.formdata.set("Id", this.editModel.id);
this.formdata.set("Name", this.form.Name);
this.formdata.set("Version", this.form.Version);
this.formdata.set("Note", this.form.Note);
this.formdata.set("FileName", this.form.FileName);
//设置请求头的Content-Type为multipart/form-data就可以传输文件
headers: { "Content-Type": "multipart/form-data" }
//axios调接口
let res = await axios.post("接口", this.formdata, config);
if (res.status == 200) {
this.$message.success("成功上传到服务器");
} else this.$message.error("上传失败");
下载文件,主要是调接口拿到文件流或其他信息,然后创建一个a标签用于下载(createElement)
//调接口
let res = await this.DownloadClient.getPersonTemplate();
let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
let href = window.URL.createObjectURL(blob);
//创建用于下载的a标签
const a = document.createElement("a");
a.href = href;
a.download = "模板.xlsx"; //下载后文件名
document.body.appendChild(a);
a.click(); //点击下载
document.body.removeChild(a); //下载完成移除元素
window.URL.revokeObjectURL(href); //释放掉blob对象