downloadFile.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { useMessage, useDialog } from 'naive-ui'
  2. import axios from 'axios'
  3. import cleanDeep from 'clean-deep'
  4. import { useUserStore } from '@/store/modules/user'
  5. /**
  6. * 根据文件url获取文件名
  7. * @param url 文件url
  8. */
  9. function getFileName(url: any) {
  10. const num = url.lastIndexOf('/') + 1
  11. let fileName = url.substring(num)
  12. //把参数和文件名分割开
  13. fileName = decodeURI(fileName.split('?')[0])
  14. return fileName
  15. }
  16. /**
  17. * 根据文件地址下载文件
  18. * @param {*} sUrl
  19. */
  20. export function downloadByUrl({
  21. url,
  22. target = '_blank',
  23. fileName
  24. }: {
  25. url: string
  26. target?: '_self' | '_blank'
  27. fileName?: string
  28. }): Promise<boolean> {
  29. // 是否同源
  30. const isSameHost = new URL(url).host == location.host
  31. return new Promise<boolean>((resolve, reject) => {
  32. if (isSameHost) {
  33. const link = document.createElement('a')
  34. link.href = url
  35. link.target = target
  36. if (link.download !== undefined) {
  37. link.download = fileName || getFileName(url)
  38. }
  39. if (document.createEvent) {
  40. const e = document.createEvent('MouseEvents')
  41. e.initEvent('click', true, true)
  42. link.dispatchEvent(e)
  43. return resolve(true)
  44. }
  45. if (url.indexOf('?') === -1) {
  46. url += '?download'
  47. }
  48. window.open(url, target)
  49. return resolve(true)
  50. } else {
  51. const canvas = document.createElement('canvas')
  52. const img = document.createElement('img')
  53. img.setAttribute('crossOrigin', 'Anonymous')
  54. img.src = url
  55. img.onload = () => {
  56. canvas.width = img.width
  57. canvas.height = img.height
  58. const context = canvas.getContext('2d')!
  59. context.drawImage(img, 0, 0, img.width, img.height)
  60. // window.navigator.msSaveBlob(canvas.msToBlob(),'image.jpg');
  61. // saveAs(imageDataUrl, '附件');
  62. canvas.toBlob((blob: any) => {
  63. const link = document.createElement('a')
  64. link.href = window.URL.createObjectURL(blob)
  65. link.download = getFileName(url)
  66. link.click()
  67. URL.revokeObjectURL(link.href)
  68. resolve(true)
  69. }, 'image/jpeg')
  70. }
  71. img.onerror = (e) => reject(e)
  72. }
  73. })
  74. }
  75. export function Exports(
  76. dialog: any,
  77. params: any,
  78. messageObject: any,
  79. message?: string,
  80. func?: Function
  81. ) {
  82. // 报表导出
  83. const messageDan = useMessage()
  84. const userStore = useUserStore()
  85. const token = userStore.getToken
  86. let url = params.url
  87. const options = {
  88. method: params.method ? params.method : 'get',
  89. headers: {
  90. Authorization: token
  91. },
  92. // params: params.params,
  93. url
  94. } as any
  95. if (options.method == 'post') {
  96. options.data = params.params
  97. } else {
  98. options.params = params.params
  99. }
  100. const dialogs = useDialog()
  101. console.log('dialogs', dialogs)
  102. dialog.warning({
  103. title: '提示',
  104. content: message || '您确定下载模板',
  105. type: 'warning',
  106. positiveText: '确定',
  107. negativeText: '取消',
  108. onPositiveClick: () => {
  109. axios(cleanDeep(options))
  110. .then((res) => {
  111. console.log(res.data, 'res.data', res.data.code)
  112. if (res.data.code != 200) {
  113. console.log(res.data.message, 'res.data.message', messageDan)
  114. messageObject.error(res.data.message)
  115. return
  116. }
  117. if (res.data.data.downloadPath) {
  118. let link = document.createElement('a')
  119. let fname = res.data.data.fileName
  120. link.href = res.data.data.downloadPath //下载文件的名字
  121. console.log(link.href)
  122. link.setAttribute('download', fname)
  123. document.body.appendChild(link)
  124. link.click()
  125. if (func) {
  126. func()
  127. }
  128. }
  129. })
  130. .catch((e) => {
  131. console.log(e)
  132. })
  133. }
  134. })
  135. }