优化文件下载

This commit is contained in:
sk1551 2021-01-29 13:46:31 +08:00
parent 399fd8f1b4
commit f7db1bcb1b
2 changed files with 47 additions and 47 deletions

View File

@ -2,46 +2,46 @@ import axios from 'axios'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
const mimeMap = { const mimeMap = {
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
zip: 'application/zip' zip: 'application/zip'
} }
const baseUrl = process.env.VUE_APP_BASE_API const baseUrl = process.env.VUE_APP_BASE_API
export function downLoadZip(str, filename) { export function downLoadZip(str, filename) {
var url = baseUrl + str var url = baseUrl + str
axios({ axios({
method: 'get', method: 'get',
url: url, url: url,
responseType: 'blob', responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() } headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(res => { }).then(res => {
resolveBlob(res, mimeMap.zip) resolveBlob(res, mimeMap.zip)
}) })
} }
export function downLoadUrl(str) { export function downLoadUrl(str, item) {
var url = baseUrl + str var url = baseUrl + str
axios({ axios({
method: 'get', method: 'get',
url: url, url: url,
responseType: 'blob', responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }, headers: { 'Authorization': 'Bearer ' + getToken() },
}).then(res => { }).then(res => {
console.log(res); // console.log(res);
// downloadFileFun(res, item) downloadFileFun(res.data, item)
}) })
} }
export function downloadFileFun(data, item) { export function downloadFileFun(data, item) {
if (!data) { if (!data) {
return return
} }
let url = window.URL.createObjectURL(new Blob([data])) let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a') let link = document.createElement('a')
link.style.display = 'none' link.style.display = 'none'
link.href = url link.href = url
link.setAttribute('download', item.name) link.setAttribute('download', item.name)
document.body.appendChild(link) document.body.appendChild(link)
link.click() link.click()
} }
/** /**
@ -50,17 +50,17 @@ export function downloadFileFun(data, item) {
* @param {String} mimeType MIME类型 * @param {String} mimeType MIME类型
*/ */
export function resolveBlob(res, mimeType) { export function resolveBlob(res, mimeType) {
const aLink = document.createElement('a') const aLink = document.createElement('a')
var blob = new Blob([res.data], { type: mimeType }) var blob = new Blob([res.data], { type: mimeType })
// //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名; // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*') var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
var contentDisposition = decodeURI(res.headers['content-disposition']) var contentDisposition = decodeURI(res.headers['content-disposition'])
var result = patt.exec(contentDisposition) var result = patt.exec(contentDisposition)
var fileName = result[1] var fileName = result[1]
fileName = fileName.replace(/\"/g, '') fileName = fileName.replace(/\"/g, '')
aLink.href = URL.createObjectURL(blob) aLink.href = URL.createObjectURL(blob)
aLink.setAttribute('download', fileName) // 设置下载文件名称 aLink.setAttribute('download', fileName) // 设置下载文件名称
document.body.appendChild(aLink) document.body.appendChild(aLink)
aLink.click() aLink.click()
document.body.appendChild(aLink) document.body.appendChild(aLink)
} }

View File

@ -67,7 +67,7 @@ export default {
}, },
load() {}, load() {},
down(row) { down(row) {
downLoadUrl(row.fileurl); downLoadUrl(row.fileurl,row);
}, },
}, },
}; };