download.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const download0 = (data: Blob, fileName: string, mineType: string) => {
  2. // 创建 blob
  3. const blob = new Blob([data], { type: mineType })
  4. // 创建 href 超链接,点击进行下载
  5. window.URL = window.URL || window.webkitURL
  6. const href = URL.createObjectURL(blob)
  7. const downA = document.createElement('a')
  8. downA.href = href
  9. downA.download = fileName
  10. downA.click()
  11. // 销毁超连接
  12. window.URL.revokeObjectURL(href)
  13. }
  14. const download = {
  15. // 下载 Excel 方法
  16. excel: (data: Blob, fileName: string) => {
  17. download0(data, fileName, 'application/vnd.ms-excel')
  18. },
  19. // 下载 Word 方法
  20. word: (data: Blob, fileName: string) => {
  21. download0(data, fileName, 'application/msword')
  22. },
  23. // 下载 Zip 方法
  24. zip: (data: Blob, fileName: string) => {
  25. download0(data, fileName, 'application/zip')
  26. },
  27. // 下载 Html 方法
  28. html: (data: Blob, fileName: string) => {
  29. download0(data, fileName, 'text/html')
  30. },
  31. // 下载 Markdown 方法
  32. markdown: (data: Blob, fileName: string) => {
  33. download0(data, fileName, 'text/markdown')
  34. },
  35. // 下载 Json 方法
  36. json: (data: Blob, fileName: string) => {
  37. download0(data, fileName, 'application/json')
  38. },
  39. // 下载图片(允许跨域)
  40. image: ({
  41. url,
  42. canvasWidth,
  43. canvasHeight,
  44. drawWithImageSize = true
  45. }: {
  46. url: string
  47. canvasWidth?: number // 指定画布宽度
  48. canvasHeight?: number // 指定画布高度
  49. drawWithImageSize?: boolean // 将图片绘制在画布上时带上图片的宽高值, 默认是要带上的
  50. }) => {
  51. const image = new Image()
  52. // image.setAttribute('crossOrigin', 'anonymous')
  53. image.src = url
  54. image.onload = () => {
  55. const canvas = document.createElement('canvas')
  56. canvas.width = canvasWidth || image.width
  57. canvas.height = canvasHeight || image.height
  58. const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
  59. ctx?.clearRect(0, 0, canvas.width, canvas.height)
  60. if (drawWithImageSize) {
  61. ctx.drawImage(image, 0, 0, image.width, image.height)
  62. } else {
  63. ctx.drawImage(image, 0, 0)
  64. }
  65. const url = canvas.toDataURL('image/png')
  66. const a = document.createElement('a')
  67. a.href = url
  68. a.download = 'image.png'
  69. a.click()
  70. }
  71. },
  72. base64ToFile: (base64: any, fileName: string) => {
  73. // 将base64按照 , 进行分割 将前缀 与后续内容分隔开
  74. const data = base64.split(',')
  75. // 利用正则表达式 从前缀中获取图片的类型信息(image/png、image/jpeg、image/webp等)
  76. const type = data[0].match(/:(.*?);/)[1]
  77. // 从图片的类型信息中 获取具体的文件格式后缀(png、jpeg、webp)
  78. const suffix = type.split('/')[1]
  79. // 使用atob()对base64数据进行解码 结果是一个文件数据流 以字符串的格式输出
  80. const bstr = window.atob(data[1])
  81. // 获取解码结果字符串的长度
  82. let n = bstr.length
  83. // 根据解码结果字符串的长度创建一个等长的整形数字数组
  84. // 但在创建时 所有元素初始值都为 0
  85. const u8arr = new Uint8Array(n)
  86. // 将整形数组的每个元素填充为解码结果字符串对应位置字符的UTF-16 编码单元
  87. while (n--) {
  88. // charCodeAt():获取给定索引处字符对应的 UTF-16 代码单元
  89. u8arr[n] = bstr.charCodeAt(n)
  90. }
  91. // 将File文件对象返回给方法的调用者
  92. return new File([u8arr], `${fileName}.${suffix}`, {
  93. type: type
  94. })
  95. }
  96. }
  97. export default download