axios 下载文件流

exportOrder(){
            let that = this
            let data = {
                page: that.page,
                status: that.status,
                q: that.searchData
            }
            axios.post(`/jmarket/admin/v1/order/export`,data,{responseType:'blob',headers: { Authorization: `bearer ${localStorage.getItem('kgconsole_token')}`}},).then(function (response){
                const blob = new Blob([response.data]);
                let downloads = document.createElement("a");
                let href = window.URL.createObjectURL(blob);
                downloads.href = href;
                let statusText = '全部'
                if (that.status === '0') {
                    statusText = '待支付'
                }
                if (that.status === '1') {
                    statusText = '已支付'
                }
                if (that.status === '2') {
                    statusText = '已取消'
                }
                downloads.download =  `${that.searchData}${statusText}订单.xlsx`;
                document.body.appendChild(downloads);
                downloads.click();
                document.body.removeChild(downloads);
                window.URL.revokeObjectURL(href);
            }).catch(function (error){
                that.$message.error(error)
            });
        },

 

posted @ 2024-09-11 12:09  Brian_白  阅读(58)  评论(0)    收藏  举报