diff --git a/src/utils/curd.js b/src/utils/curd.js index 0fe3334c..d28337aa 100644 --- a/src/utils/curd.js +++ b/src/utils/curd.js @@ -1,13 +1,11 @@ -/** -Created by hedekun -20180727 - */ +import { ExcelDownLoad } from '@/utils/request' + import { type } from './tool' // type判断传入值的类型,比typeof更详细 export default { install(Vue) { - Vue.prototype.exportExcel_NPOI = function(url, type, name, data) { - console.log(url, type, name, data, 1111111111) - // this.download(`${url}?param=${data}`) + Vue.prototype.exportExcel_NPOI = function(param) { + console.log(ExcelDownLoad, param, 1111111111) + download(`${ExcelDownLoad}?param=${param}`) } Vue.prototype.CreateData = function(type, name, data, pageSize, pageList) { var paramStr = '' @@ -272,40 +270,39 @@ export default { } } } +function download(url) { + getBlob(url, function(blob, filename) { + saveAs(blob, filename) + }) +} +// function decodeUtf8(bytes) { var encoded = ''; for (var i = 0; i < bytes.length; i++) { encoded += '%' + bytes[i].toString(16) } return decodeURIComponent(encoded) } +function getBlob(url, cb) { + var xhr = new XMLHttpRequest() + xhr.open('GET', url, true) + xhr.responseType = 'blob' + xhr.onload = function() { + if (xhr.status === 200) { + const name = xhr.getResponseHeader('content-disposition').split('=')[1] + var filename1 = decodeURIComponent(name) + cb(xhr.response, filename1) + } + } + xhr.send() +} -// function download(url) { -// getBlob(url, function(blob, filename) { -// var filename1 = decodeURIComponent(filename) -// saveAs(blob, filename1) -// }) -// } -// -// function decodeUtf8(bytes) { var encoded = ""; for (var i = 0; i < bytes.length; i++) { encoded += '%' + bytes[i].toString(16); } return decodeURIComponent(encoded); } -// -// function getBlob(url, cb) { -// var xhr = new XMLHttpRequest() -// xhr.open('GET', url, true) -// xhr.responseType = 'blob' -// xhr.onload = function() { -// if (xhr.status === 200) { -// var name = decodeUtf8(xhr.getResponseHeader('content-disposition').split('=')[1]) -// } -// } -// xhr.send() -// } -// -// function saveAs(blob, filename = '下载文件.xls') { -// if (window.navigator.msSaveOrOpenBlob) { -// navigator.msSaveBlob(blob, filename) -// } else { -// var link = document.createElement('a') -// var body = document.querySelector('body') -// link.href = window.URL.createObjectURL(blob) -// link.download = filename -// link.style.display = 'none' -// body.appendChild(link) -// link.click() -// body.removeChild(link) -// window.URL.revokeObjectURL(link.href) -// } -// } +function saveAs(blob, filename = '下载文件.xls') { + if (window.navigator.msSaveOrOpenBlob) { + navigator.msSaveBlob(blob, filename) + } else { + var link = document.createElement('a') + var body = document.querySelector('body') + link.href = window.URL.createObjectURL(blob) + link.download = filename + // fix Firefox + link.style.display = 'none' + body.appendChild(link) + link.click() + body.removeChild(link) + window.URL.revokeObjectURL(link.href) + } +}