filt.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. export const openWindow = (
  2. url: string,
  3. opt?: {
  4. target?: '_self' | '_blank' | string
  5. noopener?: boolean
  6. noreferrer?: boolean
  7. }
  8. ) => {
  9. const { target = '__blank', noopener = true, noreferrer = true } = opt || {}
  10. const feature: string[] = []
  11. noopener && feature.push('noopener=yes')
  12. noreferrer && feature.push('noreferrer=yes')
  13. window.open(url, target, feature.join(','))
  14. }
  15. /**
  16. * @description: base64 to blob
  17. */
  18. export const dataURLtoBlob = (base64Buf: string): Blob => {
  19. const arr = base64Buf.split(',')
  20. const typeItem = arr[0]
  21. const mime = typeItem.match(/:(.*?);/)![1]
  22. const bstr = window.atob(arr[1])
  23. let n = bstr.length
  24. const u8arr = new Uint8Array(n)
  25. while (n--) {
  26. u8arr[n] = bstr.charCodeAt(n)
  27. }
  28. return new Blob([u8arr], { type: mime })
  29. }
  30. /**
  31. * img url to base64
  32. * @param url
  33. */
  34. export const urlToBase64 = (url: string, mineType?: string): Promise<string> => {
  35. return new Promise((resolve, reject) => {
  36. let canvas = document.createElement('CANVAS') as Nullable<HTMLCanvasElement>
  37. const ctx = canvas!.getContext('2d')
  38. const img = new Image()
  39. img.crossOrigin = ''
  40. img.onload = function () {
  41. if (!canvas || !ctx) {
  42. return reject()
  43. }
  44. canvas.height = img.height
  45. canvas.width = img.width
  46. ctx.drawImage(img, 0, 0)
  47. const dataURL = canvas.toDataURL(mineType || 'image/png')
  48. canvas = null
  49. resolve(dataURL)
  50. }
  51. img.src = url
  52. })
  53. }
  54. /**
  55. * Download online pictures
  56. * @param url
  57. * @param filename
  58. * @param mime
  59. * @param bom
  60. */
  61. export const downloadByOnlineUrl = (
  62. url: string,
  63. filename: string,
  64. mime?: string,
  65. bom?: BlobPart
  66. ) => {
  67. urlToBase64(url).then((base64) => {
  68. downloadByBase64(base64, filename, mime, bom)
  69. })
  70. }
  71. /**
  72. * Download pictures based on base64
  73. * @param buf
  74. * @param filename
  75. * @param mime
  76. * @param bom
  77. */
  78. export const downloadByBase64 = (buf: string, filename: string, mime?: string, bom?: BlobPart) => {
  79. const base64Buf = dataURLtoBlob(buf)
  80. downloadByData(base64Buf, filename, mime, bom)
  81. }
  82. /**
  83. * Download according to the background interface file stream
  84. * @param {*} data
  85. * @param {*} filename
  86. * @param {*} mime
  87. * @param {*} bom
  88. */
  89. export const downloadByData = (data: BlobPart, filename: string, mime?: string, bom?: BlobPart) => {
  90. const blobData = typeof bom !== 'undefined' ? [bom, data] : [data]
  91. const blob = new Blob(blobData, { type: mime || 'application/octet-stream' })
  92. const blobURL = window.URL.createObjectURL(blob)
  93. const tempLink = document.createElement('a')
  94. tempLink.style.display = 'none'
  95. tempLink.href = blobURL
  96. tempLink.setAttribute('download', filename)
  97. if (typeof tempLink.download === 'undefined') {
  98. tempLink.setAttribute('target', '_blank')
  99. }
  100. document.body.appendChild(tempLink)
  101. tempLink.click()
  102. document.body.removeChild(tempLink)
  103. window.URL.revokeObjectURL(blobURL)
  104. }
  105. /**
  106. * Download file according to file address
  107. * @param {*} sUrl
  108. */
  109. export const downloadByUrl = ({
  110. url,
  111. target = '_blank',
  112. fileName
  113. }: {
  114. url: string
  115. target?: '_self' | '_blank'
  116. fileName?: string
  117. }): boolean => {
  118. const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1
  119. const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1
  120. if (/(iP)/g.test(window.navigator.userAgent)) {
  121. console.error('Your browser does not support download!')
  122. return false
  123. }
  124. if (isChrome || isSafari) {
  125. const link = document.createElement('a')
  126. link.href = url
  127. link.target = target
  128. if (link.download !== undefined) {
  129. link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length)
  130. }
  131. if (document.createEvent) {
  132. const e = document.createEvent('MouseEvents')
  133. e.initEvent('click', true, true)
  134. link.dispatchEvent(e)
  135. return true
  136. }
  137. }
  138. if (url.indexOf('?') === -1) {
  139. url += '?download'
  140. }
  141. openWindow(url, { target })
  142. return true
  143. }