共计 1150 个字符,预计需要花费 3 分钟才能阅读完成。
上传采纳的是 FormData https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader/readAsDataURL
下载采纳的是 a 标签的模式
import Axios from 'axios'
class Utils {downloadFile(url, params) {
Axios.get(url, {
params,
responseType: "blob",
headers: {Authorization: localStorage.getItem("token"),
}
}).then(res => {if (res.data) {let urls = window.URL.createObjectURL(res.data);
let link = document.createElement("a");
link.style.display = "none";
link.setAttribute("download", "export.xls");
link.href = urls;
document.body.appendChild(link);
link.click();}
})
}
uploadFile(file, url) {return new Promise((resolve, reject) => {if (file) {let formData = new FormData();
formData.append("file", file);
Axios.post(url, formData, {
headers: {
"Content-type": "multipart/form-data",
Authorization: localStorage.getItem("token"),
}
}).then(resolve).catch(reject)
} else {reject(new Error('没有上传文件!'))
}
})
}
}
export default new Utils();
import Utils from ‘../../Utils/Utils.js’
应用:
// 下载
let obj = Object.assign({}, this.queryInfo);
// download.downloadFile("api/manager/corInvoice/export",obj)
// 上传
download.uploadFile(file, "api/manager/corInvoice/import")
.then((res) => {if (res.status == 200) {this.$message.success("导入文件胜利!");
this.initTable();}
})
.catch((err) => {console.log(err);
});
正文完