vue blob excel導出數據

    downloadFile (res, fileName) {
        let data = res.data
        let url = window.URL.createObjectURL(new Blob([data], {
            type: "application/vnd.ms-excel"
        }))
        if ("download" in document.createElement("a")) {
            const a = document.createElement("a")
            a.href = url
            a.download = fileName
            a.style.display = "none"
            document.body.appendChild(a)
            a.click()
            URL.revokeObjectURL(a.href)
            document.body.removeChild(a)
        }
    },
    exportData() {
        exportData('/ih/statistic/consultOrder/excel', this.params).then(res => {
            if (res) {
                let fileName = "諮詢訂單統計表.xlsx"        
                this.downloadFile(res, fileName)
            } else {
                this.$message({
                    type: 'error',
                    message: res.msg
                })
            }
        })
    },

補充一下,導出失敗時,json信息如何提示

        if (type == 'application/vnd.ms-excel') {
            return res;
        } else if (type == 'application/json') {
            const reader = new FileReader()
            reader.readAsText(data)
            reader.onload = function (e) {
                let obj = JSON.parse(e.target.result)//此處的msg就是後端返回的msg內容
                if (obj) {
                    data = obj
                }
                var logoutCodeArr = [610, 611];
                if (~logoutCodeArr.indexOf(data.code)) {
                    var loginPath = getLoginPath();
                    vm.$router.push(loginPath);
                }
                if (data.msg) {
                    alerts(data.msg);
                }
            }
        } else {
            var err = new Error();
            err.msg = "服務器內部錯誤";
            return Promise.reject(err);
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章