vue中使用axios下載文件,兼容IE11

1、設置axios返回值爲blobios

2、使用a標籤的down屬性下載,若是是IE瀏覽器,可使用navigator.msSaveBlob進行下載axios

// 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' in window) || '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()
  }
}
相關文章
相關標籤/搜索