上傳采用的是FormData https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader/readAsDataURL
下載采用的是a標(biāo)簽的形式
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("導(dǎo)入文件成功诬乞!");
this.initTable();
}
})
.catch((err) => {
console.log(err);
});