Commit 9a57e68f by xinla

提取公用下载blob数据方法downloadBlob等

1 parent fff4f329
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
const baseURL = window.lwConfig.BASE_API const baseURL = window.lwConfig.BASE_API
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import { dateFormat } from '@/utils/index'
import axios from 'axios' import axios from 'axios'
// 日期格式化 // 日期格式化
...@@ -95,6 +96,28 @@ export function selectDictLabels(datas, value, separator) { ...@@ -95,6 +96,28 @@ export function selectDictLabels(datas, value, separator) {
return actions.join('').substring(0, actions.join('').length - 1) return actions.join('').substring(0, actions.join('').length - 1)
} }
/**
* 下载bolb文件
* @param {*} blob bolo源数据
* @param {*} type 文件类型
* @param {*} downloadName 下载文件名,需含文件后缀名
*/
export function downloadBlob(blob, type, downloadName) {
const typeDict = {
excel: 'application/vnd.ms-excel',
zip: 'application/zip',
image: 'application/image',
}
blob = new Blob([blob], { type: typeDict[type] || type })
let url = window.URL.createObjectURL(blob)
const a = document.createElement('a') // 创建a标签
a.href = url
a.download = dateFormat(new Date(), 'YYYY-MM-DD HH:mm:ss-') + downloadName // 下载文件名,不能包含英文 : 冒号
a.click()
a.remove()
URL.revokeObjectURL(url) // 释放内存
}
// 通用下载方法 系统内部 // 通用下载方法 系统内部
export function download(fileName) { export function download(fileName) {
axios({ axios({
...@@ -106,16 +129,18 @@ export function download(fileName) { ...@@ -106,16 +129,18 @@ export function download(fileName) {
}, },
}).then((res) => { }).then((res) => {
const { data, headers } = res const { data, headers } = res
const blob = new Blob([data], { type: headers['content-type'] }) downloadBlob(data, headers['content-type'], fileName)
let dom = document.createElement('a')
let url = window.URL.createObjectURL(blob) // const blob = new Blob([data], { type: headers['content-type'] })
dom.href = url // let dom = document.createElement('a')
dom.download = fileName // let url = window.URL.createObjectURL(blob)
dom.style.display = 'none' // dom.href = url
document.body.appendChild(dom) // dom.download = fileName
dom.click() // dom.style.display = 'none'
dom.parentNode.removeChild(dom) // document.body.appendChild(dom)
window.URL.revokeObjectURL(url) // dom.click()
// dom.parentNode.removeChild(dom)
// window.URL.revokeObjectURL(url)
}) })
} }
...@@ -130,16 +155,18 @@ export function downloadNet(url, fileName) { ...@@ -130,16 +155,18 @@ export function downloadNet(url, fileName) {
}, },
}).then((res) => { }).then((res) => {
const { data, headers } = res const { data, headers } = res
const blob = new Blob([data], { type: headers['content-type'] }) downloadBlob(data, headers['content-type'], fileName)
let dom = document.createElement('a')
let url = window.URL.createObjectURL(blob) // const blob = new Blob([data], { type: headers['content-type'] })
dom.href = url // let dom = document.createElement('a')
dom.download = fileName // let url = window.URL.createObjectURL(blob)
dom.style.display = 'none' // dom.href = url
document.body.appendChild(dom) // dom.download = fileName
dom.click() // dom.style.display = 'none'
dom.parentNode.removeChild(dom) // document.body.appendChild(dom)
window.URL.revokeObjectURL(url) // dom.click()
// dom.parentNode.removeChild(dom)
// window.URL.revokeObjectURL(url)
}) })
} }
......
...@@ -53,7 +53,7 @@ export default defineConfig(async ({ command, mode }) => { ...@@ -53,7 +53,7 @@ export default defineConfig(async ({ command, mode }) => {
}, },
// vite 相关配置 // vite 相关配置
server: { server: {
port: 1024, port: { development: 1024, test: 1025 }[mode],
host: true, host: true,
open: true, open: true,
proxy: { proxy: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!