1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| import { Loading, Message, MessageBox } from "element-ui"
export function downLoadExcel(params) { if (params && params.url) { let message = (params && params.loadingText) ? params.loadingText : '文件数据资源下载中...' Loading.service({ fullscreen: true, text: message, background: 'rgba(0, 0, 0, 0.7)', lock: true }) setTimeout(function () { let xhr = new XMLHttpRequest() let urls = params.url xhr.open('get', urls, true) xhr.responseType = 'blob' xhr.timeout = params.timeout ? params.timeout : (15 * 60 * 1000) xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8') xhr.onreadystatechange = function () { if (this.readyState == 4) { if (this.status == 200) { let fileName = this.getResponseHeader('content-disposition') if (fileName) { let indexName = fileName.indexOf('filename') fileName = decodeURIComponent(fileName.slice(indexName + 9, fileName.length)).replace(/"/g, '') let blob = this.response let blobURL = window.webkitURL.createObjectURL(blob) let a = document.createElement('a') a.download = fileName a.href = blobURL $('body').append(a) a.click() $(a).remove() Loading.service({ fullscreen: true }).close() } else { let resultMsg = null let reader = null if (window.FileReader) { reader = new FileReader() reader.onload = function () { if (reader.readyState == 2) { let resultJson = JSON.parse(reader.result) resultJson && (resultMsg = resultJson.msg || resultJson.message) Loading.service({ fullscreen: true }).close() MessageBox.confirm(resultMsg || '文件不存在,请确认后重试!', '温馨提示', { confirmButtonText: '确认', showCancelButton: false, type: 'warning' }).catch(err => { }) } } reader.readAsText(this.response) } } } else { let resMsg = this.responseText || '' Loading.service({ fullscreen: true }).close() MessageBox.confirm('文件下载失败:' + resMsg, '温馨提示', { confirmButtonText: '确认', showCancelButton: false, type: 'warning' }).catch(err => { }) } } else { Loading.service({ fullscreen: true }).close() } } xhr.send() }, 500) } else { MessageBox.confirm('文件地址不存在', '温馨提示', { confirmButtonText: '确认', showCancelButton: false, type: 'warning' }).catch(err => { }) } }
|