download.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import axios from 'axios'
  2. import { getToken } from '@/utils/auth'
  3. const mimeMap = {
  4. xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  5. zip: 'application/zip',
  6. oss: 'application/octet-stream'
  7. }
  8. const baseUrl = process.env.VUE_APP_BASE_API
  9. export function downLoadZip(str, filename) {
  10. var url = baseUrl + str
  11. axios({
  12. method: 'get',
  13. url: url,
  14. responseType: 'blob',
  15. headers: { 'Authorization': 'Bearer ' + getToken() }
  16. }).then(res => {
  17. resolveBlob(res, mimeMap.zip)
  18. })
  19. }
  20. export function downLoadOss(ossId) {
  21. var url = baseUrl + '/system/oss/download/' + ossId
  22. axios({
  23. method: 'get',
  24. url: url,
  25. responseType: 'blob',
  26. headers: { 'Authorization': 'Bearer ' + getToken() }
  27. }).then(res => {
  28. resolveBlob(res, mimeMap.oss)
  29. })
  30. }
  31. export function downLoadExcel(url, params) {
  32. // get请求映射params参数
  33. if (params) {
  34. let urlparams = url + '?';
  35. for (const propName of Object.keys(params)) {
  36. const value = params[propName];
  37. var part = encodeURIComponent(propName) + "=";
  38. if (value !== null && typeof(value) !== "undefined") {
  39. if (typeof value === 'object') {
  40. for (const key of Object.keys(value)) {
  41. let params = propName + '[' + key + ']';
  42. var subPart = encodeURIComponent(params) + "=";
  43. urlparams += subPart + encodeURIComponent(value[key]) + "&";
  44. }
  45. } else {
  46. urlparams += part + encodeURIComponent(value) + "&";
  47. }
  48. }
  49. }
  50. urlparams = urlparams.slice(0, -1);
  51. url = urlparams;
  52. }
  53. url = baseUrl + url
  54. axios({
  55. method: 'get',
  56. url: url,
  57. responseType: 'blob',
  58. headers: { 'Authorization': 'Bearer ' + getToken() }
  59. }).then(res => {
  60. resolveBlob(res, mimeMap.xlsx)
  61. })
  62. }
  63. /**
  64. * 解析blob响应内容并下载
  65. * @param {*} res blob响应内容
  66. * @param {String} mimeType MIME类型
  67. */
  68. export function resolveBlob(res, mimeType) {
  69. const aLink = document.createElement('a')
  70. var blob = new Blob([res.data], { type: mimeType })
  71. // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
  72. var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
  73. var contentDisposition = decodeURI(res.headers['content-disposition'])
  74. var result = patt.exec(contentDisposition)
  75. var fileName = result[1]
  76. fileName = fileName.replace(/\"/g, '')
  77. aLink.style.display = 'none'
  78. aLink.href = URL.createObjectURL(blob)
  79. aLink.setAttribute('download', decodeURI(fileName)) // 设置下载文件名称
  80. document.body.appendChild(aLink)
  81. aLink.click()
  82. URL.revokeObjectURL(aLink.href);//清除引用
  83. document.body.removeChild(aLink);
  84. }