vue中使用axios下載檔案,相容IE11
阿新 • • 發佈:2018-12-12
一、設定axios返回值為blob
二、使用a標籤的down屬性下載,如果是IE瀏覽器,可以使用navigator.msSaveBlob進行下載
// data的資料型別是blob downloadFiles (data) { if (!data) { return } const uA = window.navigator.userAgent const isIE = /msie\s|trident\/|edge\//i.test(uA) && !!('uniqueID' in document || 'documentMode' in document || ('ActiveXObject' inwindow) || 'MSInputMethodContext' in window) let url = window.URL.createObjectURL(new Blob([data])) let link = document.createElement('a') link.style.display = 'none' link.href = url link.setAttribute('download', '消費碼.zip') document.body.appendChild(link) // 相容IE if (isIE) { navigator.msSaveBlob(new Blob([data]), '消費碼.zip') } else { link.click() } }