download.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  42. let params = propName + '[' + key + ']';
  43. let subPart = encodeURIComponent(params) + '=';
  44. urlparams += subPart + encodeURIComponent(value[key]) + '&';
  45. }
  46. }
  47. } else {
  48. urlparams += part + encodeURIComponent(value) + "&";
  49. }
  50. }
  51. }
  52. urlparams = urlparams.slice(0, -1);
  53. url = urlparams;
  54. }
  55. url = baseUrl + url
  56. axios({
  57. method: 'get',
  58. url: url,
  59. responseType: 'blob',
  60. headers: { 'Authorization': 'Bearer ' + getToken() }
  61. }).then(res => {
  62. resolveBlob(res, mimeMap.xlsx)
  63. })
  64. }
  65. /**
  66. * 解析blob响应内容并下载
  67. * @param {*} res blob响应内容
  68. * @param {String} mimeType MIME类型
  69. */
  70. export function resolveBlob(res, mimeType) {
  71. const aLink = document.createElement('a')
  72. var blob = new Blob([res.data], { type: mimeType })
  73. // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
  74. var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
  75. var contentDisposition = decodeURI(res.headers['content-disposition'])
  76. var result = patt.exec(contentDisposition)
  77. var fileName = result[1]
  78. fileName = fileName.replace(/\"/g, '')
  79. aLink.style.display = 'none'
  80. aLink.href = URL.createObjectURL(blob)
  81. aLink.setAttribute('download', decodeURI(fileName)) // 设置下载文件名称
  82. document.body.appendChild(aLink)
  83. aLink.click()
  84. URL.revokeObjectURL(aLink.href);//清除引用
  85. document.body.removeChild(aLink);
  86. }