广州市综治平台前端
xusd
17 hours ago d880dfb0f9a79c51834c9f8a7017d8fa59f14f36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import axios from 'axios'
import qs from 'qs'
import * as $$ from '@/utils/utility';
 
export default function downloadFile(url, params) {
    const link = document.createElement('a')
    return axios({
            url,
            params,
            method: 'GET',
            headers: {
                Authorization: $$.getSessionStorage('customerSystemToken')
            },
            responseType: 'blob',
            paramsSerializer: (v) => qs.stringify(v, {
                arrayFormat: 'repeat'
            }),
        })
        .then((res) => {
            // 切割文件名
            const fileNameEncode = res.headers['content-disposition'].split('filename=')[1]
            // 解码
            const fileName = decodeURIComponent(fileNameEncode)
            // 设置type类型
            const blob = new Blob([res.data], {
                type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; application/octet-stream',
            })
            const fileUrl = window.URL.createObjectURL(blob)
            link.href = fileUrl
            link.setAttribute('download', fileName)
            link.style.display = 'none'
            link.click()
            link.remove()
        })
        .catch(() => {
            $$.info({
                type: 'error',
                content: '文件下载请求失败'
            });
        })
}