import { openWindow } from '@/main'; import { dataURLtoBlob, urlToBase64 } from './base64Conver'; /** * Download online pictures * @param url * @param filename * @param mime * @param bom */ export function downloadByOnlineUrl(url: string, filename: string, mime?: string, bom?: BlobPart) { console.log('downloadByOnlineUrl'); urlToBase64(url).then((base64) => { console.log('base64', base64); downloadByBase64(base64, filename, mime, bom); }); } /** * Download pictures based on base64 * @param buf * @param filename * @param mime * @param bom */ export function downloadByBase64(buf: string, filename: string, mime?: string, bom?: BlobPart) { const base64Buf = dataURLtoBlob(buf); console.log('base64Buf', base64Buf); downloadByData(base64Buf, filename, mime, bom); } /** * Download according to the background interface file stream * @param {*} data * @param {*} filename * @param {*} mime * @param {*} bom */ export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) { const blobData = typeof bom !== 'undefined' ? [bom, data] : [data]; const blob = new Blob(blobData, { type: mime || 'application/octet-stream' }); const blobURL = window.URL.createObjectURL(blob); const tempLink = document.createElement('a'); tempLink.style.display = 'none'; tempLink.href = blobURL; tempLink.setAttribute('download', filename); if (typeof tempLink.download === 'undefined') { tempLink.setAttribute('target', '_blank'); } document.body.appendChild(tempLink); tempLink.click(); document.body.removeChild(tempLink); window.URL.revokeObjectURL(blobURL); } /** * Download file according to file address * @param {*} sUrl */ export function downloadByUrl({ url, target = '_blank', fileName, }: { url: string; target?: TargetContext; fileName?: string; }): boolean { const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1; const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1; if (/(iP)/g.test(window.navigator.userAgent)) { console.error('Your browser does not support download!'); return false; } if (isChrome || isSafari) { const link = document.createElement('a'); link.href = url; link.target = target; link.setAttribute('referrerPolicy', 'origin'); if (link.download !== undefined) { link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length); } if (document.createEvent) { const e = document.createEvent('MouseEvents'); e.initEvent('click', true, true); link.dispatchEvent(e); return true; } } if (url.indexOf('?') === -1) { url += '?download'; } openWindow(url, { target }); return true; } function getBlob(url) { // url:是文件在oss上的地址 return new Promise((resolve) => { const xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'blob'; // 请求类型是blob类型 // xhr.crossOrigin = '*'; // 解决跨域问题 xhr.onload = () => { if (xhr.status === 200) { resolve(xhr.response); } }; xhr.send(); }); } function saveAs(blob, filename) { if (window.navigator.userAgent.includes('MSIE ') || window.navigator.userAgent.includes('Trident/') || window.navigator.userAgent.includes('Edge/')) { // 仅在IE或旧Edge中执行 (window.navigator as any).msSaveOrOpenBlob(blob, filename); } else { // 其他现代浏览器的处理方式 const link = document.createElement('a'); const url = window.URL.createObjectURL(blob); link.href = url; link.download = filename; link.style.display = 'none'; document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); } } export const downloadByA = (url, fileName) => { getBlob(url).then((res) => { //url:文件在oss上的地址 saveAs(res, fileName); // filename:文件名,可自定义 }); // return axios // .get(url, { // responseType: 'blob', // }) // .then((res) => { // const blob = new Blob([res]); // const href = window.URL.createObjectURL(blob); // const a = document.createElement('a'); // a.setAttribute('href', href); // a.setAttribute('download', `${fileName}`); // document.body.appendChild(a); // a.click(); // document.body.removeChild(a); // 下载完成移除元素 // window.URL.revokeObjectURL(href); // 释放掉blob对象 // }); };