一.創(chuàng)建exports.js文件
export default {
// 導(dǎo)出
exports(data,url,name='光儲(chǔ)充能源一體化') {
//data 接口參數(shù)
//url 接口地址
// name 導(dǎo)出的文件夾名稱
function createObjectURL(object) {
return (window.URL) ? window.URL.createObjectURL(object) : window.webkitURL.createObjectURL(object);
}
let xhr = new XMLHttpRequest();
let day = new Date(); //創(chuàng)建當(dāng)前時(shí)間
xhr.open('post', url);
xhr.setRequestHeader("Content-type","application/json")
xhr.setRequestHeader("Authorization", sessionStorage.getItem('Authorization')); //設(shè)置token
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
let blob = this.response;
let filename = name + day.toLocaleDateString(); //文件名+當(dāng)前時(shí)間
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename);
} else {
let a = document.createElement('a');
let url = createObjectURL(blob);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
}
}
};
xhr.send(data);
},
}
二.需要的頁面移入exports.js文件
<template>
<div>
<el-button @click="exportsClick"> </el-button>
</div>
</template>
<script>
import Exs from '../../utils/exports.js'
export default {
data() {
return {}
},
methods: {
exportsClick() { //導(dǎo)出
let name='電站臺(tái)賬'; //定義文件名稱
let da = [ "1","2" ]; // da 為接口參數(shù)( 需要導(dǎo)出數(shù)據(jù)的 id )
exs.exports(JSON.stringify( da ), '/power/exportPower',name)
},
}
}
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。