常规下载
mport { Loading } from 'element-ui'
const instance = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 5000
})
export function download(url, data, fileName) {
return new Promise((resolve, reject) => {
const loading = Loading.service({
text: '正在下载数据...',
background: 'rgba(0, 0, 0, 0.7)'
})
instance.request({
url: url,
method: 'post',
data: data,
timeout: 1200000,
responseType: 'blob'
}).then(res => {
loading.close()
const blob = new Blob([res.data], {
type: 'application/vnd.ms-excel'
})
let link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.setAttribute('download', fileName)
link.click()
link = null
Message.success('导出成功!')
}).catch(err => {
loading.close()
reject(err)
})
})
}
import {download } from '@/utils/request'
export function exportExcel(data) {
return download('url', data, '导出的数据.xlsx')
}
download(this.data)
带进度条下载
import store from '@/store'
downFileProgress =(url,parameter,callback,totalSize,uniSign) =>{
return axios({
url: url,
params: parameter,
method:'get' ,
responseType: 'blob',
onDownloadProgress (progress) {
let total =progress.total|| progress.srcElement.getResponseHeader('Content-Length')
downProgress = (100 * progress.loaded / total).toFixed(2)
store.commit('downLoadProgress/SET_PROGRESS', { path: uniSign, 'progress': downProgress })
}
})
}
- 新建src\store\modules\downLoadProgress.js文件
const state = {
progressList: [],
progressError: '',
isDown:false
}
const mutations = {
SET_PROGRESS: (state, progressObj) => {
if (state.progressList.length) {
if (state.progressList.find(item => item.path == progressObj.path)) {
state.progressList.find(item => item.path == progressObj.path).progress = progressObj.progress
}
} else {
state.progressList.push(progressObj)
}
},
DEL_PROGRESS: (state, props) => {
state.progressList.splice(state.progressList.findIndex(item => item.path == props), 1)
},
CHANGE_SETTING: (state, { key, value }) => {
if (state.hasOwnProperty(key)) {
state[key] = value
}
},
updateDown(state, val) {
state.isDown = val
}
}
const actions = {
changeSetting({ commit }, data) {
commit('CHANGE_SETTING', data)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
import Vue from 'vue'
import Vuex from 'vuex'
import downLoadProgress from './modules/downLoadProgress'
import getters from './getters'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
downLoadProgress
},
state: {
},
mutations: {
},
getters
})
export default store
- 新建src\components\downLoadNotice\index.vue文件
<template>
</template>
<script>
import { mapState } from 'vuex'
export default {
name: 'downLoadNotice',
computed: {
...mapState({
'progressList': state => state.downLoadProgress.progressList
})
},
data() {
return {
notify: {}
}
},
watch: {
progressList: {
handler(n) {
let data = JSON.parse(JSON.stringify(n))
data.forEach(item => {
const domList = [...document.getElementsByClassName(item.path)]
if (domList.find(i => i.className == item.path)) {
if(item.progress)domList.find(i => i.className == item.path).innerHTML = item.progress + '%'
if (item.progress === null) {
this.$store.commit('downLoadProgress/DEL_PROGRESS', item.path)
this.$notify.error({
title: '错误',
message: '文件下载失败!'
});
}
} else {
this.notify[item.path] = this.$notify.success({
dangerouslyUseHTMLString: true,
customClass: 'progress-notify',
message: `<p style="width: 130px;">正在下载<span class="${item.path}" style="float: right">${item.progress}%</span></p>`,
showClose: false,
duration: 0
})
}
this.$store.commit('updateDown', true)
console.log(item.progress + '%', '-------------------------->')
if (item.progress == 100) {
this.notify[item.path].close()
setTimeout(() => {
delete this.notify[item.path]
}, 1000)
this.$store.commit('updateDown', false)
this.$store.commit('downLoadProgress/DEL_PROGRESS', item.path)
}
})
},
deep: true
}
}
}
</script>
<style lang="scss" scoped>
</style>
<down-load-notice></down-load-notice>
import downLoadNotice from "@/components/downLoadNotice/index"
components: { downLoadNotice },