疯狂的狮子li
2021-07-20 ed197ce7acddf4183b8459b57f0c39b0284b98b5
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
import axios from 'axios'
import { getToken } from '@/utils/auth'
 
const mimeMap = {
  oss: 'application/octet-stream'
}
 
const baseUrl = process.env.VUE_APP_BASE_API
export function downLoadOss(ossId, filename) {
  var url = baseUrl + '/system/oss/download/' + ossId
    axios({
    method: 'get',
    url: url,
    responseType: 'blob',
    headers: { 'Authorization': 'Bearer ' + getToken() }
  }).then(res => {
    resolveBlob(res, mimeMap.oss, filename)
  })
}
/**
 * 解析blob响应内容并下载
 * @param {*} res blob响应内容
 * @param {String} mimeType MIME类型
 */
export function resolveBlob(res, mimeType, filename) {
  const aLink = document.createElement('a')
  var blob = new Blob([res.data], { type: mimeType })
  aLink.href = URL.createObjectURL(blob)
  aLink.setAttribute('download', filename) // 设置下载文件名称
  document.body.appendChild(aLink)
  aLink.click()
  document.body.removeChild(aLink);
}