download.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. downloadAll:(url:string, filename:string)=>{
  41. downloadAllFile(url, filename).then(_ =>{})
  42. },
  43. // 下载图片(允许跨域)
  44. image: ({
  45. url,
  46. canvasWidth,
  47. canvasHeight,
  48. drawWithImageSize = true
  49. }: {
  50. url: string
  51. canvasWidth?: number // 指定画布宽度
  52. canvasHeight?: number // 指定画布高度
  53. drawWithImageSize?: boolean // 将图片绘制在画布上时带上图片的宽高值, 默认是要带上的
  54. }) => {
  55. const image = new Image()
  56. // image.setAttribute('crossOrigin', 'anonymous')
  57. image.src = url
  58. image.onload = () => {
  59. const canvas = document.createElement('canvas')
  60. canvas.width = canvasWidth || image.width
  61. canvas.height = canvasHeight || image.height
  62. const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
  63. ctx?.clearRect(0, 0, canvas.width, canvas.height)
  64. if (drawWithImageSize) {
  65. ctx.drawImage(image, 0, 0, image.width, image.height)
  66. } else {
  67. ctx.drawImage(image, 0, 0)
  68. }
  69. const url = canvas.toDataURL('image/png')
  70. const a = document.createElement('a')
  71. a.href = url
  72. a.download = 'image.png'
  73. a.click()
  74. }
  75. },
  76. base64ToFile: (base64: any, fileName: string) => {
  77. // 将base64按照 , 进行分割 将前缀 与后续内容分隔开
  78. const data = base64.split(',')
  79. // 利用正则表达式 从前缀中获取图片的类型信息(image/png、image/jpeg、image/webp等)
  80. const type = data[0].match(/:(.*?);/)[1]
  81. // 从图片的类型信息中 获取具体的文件格式后缀(png、jpeg、webp)
  82. const suffix = type.split('/')[1]
  83. // 使用atob()对base64数据进行解码 结果是一个文件数据流 以字符串的格式输出
  84. const bstr = window.atob(data[1])
  85. // 获取解码结果字符串的长度
  86. let n = bstr.length
  87. // 根据解码结果字符串的长度创建一个等长的整形数字数组
  88. // 但在创建时 所有元素初始值都为 0
  89. const u8arr = new Uint8Array(n)
  90. // 将整形数组的每个元素填充为解码结果字符串对应位置字符的UTF-16 编码单元
  91. while (n--) {
  92. // charCodeAt():获取给定索引处字符对应的 UTF-16 代码单元
  93. u8arr[n] = bstr.charCodeAt(n)
  94. }
  95. // 将File文件对象返回给方法的调用者
  96. return new File([u8arr], `${fileName}.${suffix}`, {
  97. type: type
  98. })
  99. }
  100. }
  101. const downloadAllFile= async (url: string, filename: string) => {
  102. try {
  103. const response = await fetch(url);
  104. if (!response.ok) {
  105. throw new Error(`Network response was not ok: ${response.statusText}`);
  106. }
  107. const blob = await response.blob();
  108. const urlObject = URL.createObjectURL(blob);
  109. const a = document.createElement('a');
  110. a.style.display = 'none';
  111. a.href = urlObject;
  112. a.download = filename;
  113. document.body.appendChild(a);
  114. a.click();
  115. setTimeout(() => {
  116. document.body.removeChild(a);
  117. window.URL.revokeObjectURL(urlObject);
  118. }, 0);
  119. } catch (error) {
  120. console.error('There was a problem with the fetch operation:', error);
  121. }
  122. }
  123. export default download