download.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { objTypeOf, blobToText } from "./utils";
  2. function parseDownloadFilename(dispositionInfo) {
  3. console.log(dispositionInfo);
  4. const strs = dispositionInfo.split(";");
  5. let filename = "";
  6. strs
  7. .map((item) => item.split("="))
  8. .find((item) => {
  9. if (item[0].indexOf("filename") !== -1) {
  10. filename = decodeURI(item[1]);
  11. }
  12. });
  13. return filename;
  14. }
  15. /**
  16. * 通过api下载文件
  17. * @param {AxiosPromise} fetchFunc 下载接口
  18. * @param {String}} fileName 文件名
  19. * @returns Promise<Boolean>
  20. */
  21. export async function downloadByApi(fetchFunc, fileName) {
  22. let errorInfo = null;
  23. const res = await fetchFunc().catch((e) => {
  24. errorInfo = e;
  25. });
  26. // 展示后台错误信息
  27. if (errorInfo && objTypeOf(errorInfo) === "blob") {
  28. const res = await blobToText(errorInfo).catch(() => {});
  29. if (!res) return Promise.reject("下载失败!");
  30. const resJson = JSON.parse(res);
  31. return Promise.reject(resJson.message);
  32. }
  33. const filename =
  34. fileName || parseDownloadFilename(res.headers["content-disposition"]);
  35. downloadByBlob(new Blob([res.data]), filename);
  36. return true;
  37. }
  38. /**
  39. * 下载blob
  40. * @param {Blob} data blob对象
  41. * @param {String} filename 文件名
  42. */
  43. export function downloadByBlob(data, filename) {
  44. const blobUrl = URL.createObjectURL(data);
  45. downloadByUrl(blobUrl, filename);
  46. }
  47. /**
  48. * 下载url
  49. * @param {String} url 文件下载地址
  50. * @param {String}} filename 文件名
  51. */
  52. export function downloadByUrl(url, filename) {
  53. const tempLink = document.createElement("a");
  54. tempLink.style.display = "none";
  55. tempLink.href = url;
  56. const fileName = filename || url.split("/").pop().split("?")[0];
  57. tempLink.setAttribute("download", fileName);
  58. if (tempLink.download === "undefined") {
  59. tempLink.setAttribute("target", "_blank");
  60. }
  61. document.body.appendChild(tempLink);
  62. tempLink.click();
  63. document.body.removeChild(tempLink);
  64. window.URL.revokeObjectURL(url);
  65. }
  66. /**
  67. * 下载图片
  68. * @param {String} url 图片地址
  69. * @param {String} filename 下载的文件名
  70. * @returns Promise<Boolean>
  71. */
  72. export function downloadByImgUrl(url, filename) {
  73. return new Promise((resolve, reject) => {
  74. const img = new Image();
  75. // 跨域支持,需要服务端辅助配置
  76. // img.crossOrigin = "";
  77. img.onload = function () {
  78. const canvas = document.createElement("canvas");
  79. const ctx = canvas.getContext("2d");
  80. if (!canvas || !ctx) {
  81. return reject("不支持下载!");
  82. }
  83. canvas.width = img.width;
  84. canvas.height = img.height;
  85. ctx.drawImage(img, 0, 0);
  86. canvas.toBlob((blob) => {
  87. downloadByBlob(blob, filename);
  88. resolve(true);
  89. });
  90. };
  91. img.onerror = function (e) {
  92. reject(e);
  93. };
  94. img.src = url;
  95. });
  96. }