¶Ô±ÈÐÂÎļþ |
| | |
| | | import axios from 'axios' |
| | | import { getToken } from '@/utils/auth' |
| | | |
| | | const mimeMap = { |
| | | xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| | | zip: 'application/zip' |
| | | } |
| | | |
| | | const baseUrl = process.env.VUE_APP_BASE_API |
| | | export function downLoadZip(str, filename) { |
| | | var url = baseUrl + str |
| | | axios({ |
| | | method: 'get', |
| | | url: url, |
| | | responseType: 'blob', |
| | | headers: { 'Authorization': 'Bearer ' + getToken() } |
| | | }).then(res => { |
| | | resolveBlob(res, mimeMap.zip) |
| | | }) |
| | | } |
| | | /** |
| | | * è§£æblobååºå
容并ä¸è½½ |
| | | * @param {*} res blobååºå
容 |
| | | * @param {String} mimeType MIMEç±»å |
| | | */ |
| | | export function resolveBlob(res, mimeType) { |
| | | const aLink = document.createElement('a') |
| | | var blob = new Blob([res.data], { type: mimeType }) |
| | | // //ä»responseçheadersä¸è·åfilename, å端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置çæä»¶å; |
| | | var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*') |
| | | var contentDisposition = decodeURI(res.headers['content-disposition']) |
| | | var result = patt.exec(contentDisposition) |
| | | var fileName = result[1] |
| | | fileName = fileName.replace(/\"/g, '') |
| | | aLink.href = URL.createObjectURL(blob) |
| | | aLink.setAttribute('download', fileName) // 设置ä¸è½½æä»¶åç§° |
| | | document.body.appendChild(aLink) |
| | | aLink.click() |
| | | document.body.removeChild(aLink); |
| | | } |