/ 下崽🐱 /

    1. export const download = async function (param = {}) {
    2. try {
    3. if (typeof param === 'string') {
    4. window.open(param);
    5. return;
    6. }
    7. // 处理blob下载
    8. const {url, ...otherParam} = param;
    9. let res = await axios({
    10. url,
    11. method: 'post',
    12. data: otherParam,
    13. withCredentials: true,
    14. responseType: 'blob'
    15. });
    16. const {data: blob, headers: {'content-disposition': contentDisposition}} = res;
    17. const fileName = decodeURIComponent(contentDisposition.split('=')[1], 'UTF-8');
    18. if (window.navigator.msSaveOrOpenBlob) {
    19. navigator.msSaveBlob(blob, fileName);
    20. return;
    21. }
    22. const selfURL = window.URL || window.webkitURL;
    23. url && selfURL.revokeObjectURL(url);
    24. const toBlobUrl = selfURL.createObjectURL(blob);
    25. const aEl = document.createElement('a');
    26. aEl.href = toBlobUrl;
    27. aEl.download = fileName;
    28. aEl.click();
    29. }
    30. catch (err) {
    31. message.error(`下载失败 ${err}`);
    32. }
    33. };
    34. /*************/
    35. import {download} from 'utils/request';
    36. download(downloadUrl);