
export const downloadFile = (data, fileName,mimetypes) => {var blob = new Blob([data], {type: mimetypes})var href = window.URL.createObjectURL(blob) //创建下载的链接if (window.navigator.msSaveBlob) {// ie 浏览器window.navigator.msSaveBlob(blob, fileName)} else {// 谷歌浏览器 创建a标签 添加download属性下载var downloadElement = document.createElement('a')downloadElement.href = hrefdownloadElement.target = '_blank'downloadElement.download = fileName //下载后文件名document.body.appendChild(downloadElement)downloadElement.click() //点击下载document.body.removeChild(downloadElement) //下载完成移除元素window.URL.revokeObjectURL(href) //释放掉blob对象}}
⚠️注意 :axios 请求时添加 responseType: ‘blob’,
常用 mimetypes
mimetypes mimes
“image/jpeg” “jpg”
“image/jpeg” “jpeg”
“image/png” “png”
“image/webp” “webp”
“application/vnd.ms-excel” “xls”
“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet” “xlsx”
“application/msword” “doc”
“application/vnd.openxmlformats-officedocument.wordprocessingml.document” “docx”
“application/vnd.openxmlformats-officedocument.presentationml.presentation” “pptx”
“application/vnd.ms-powerpoint” “ppt”
“application/pdf” “pdf”
