IE下載或者導出不兼容問題處理

//下載文件
export function downloadPDF(data, name = 'file.docx') {
  if (!data) {
    return
  }
  var blob = new Blob([data], {
    type:
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
  })
  if (isIE()) {
    window.navigator.msSaveOrOpenBlob(blob, name)
    return
  }
  var url = window.URL.createObjectURL(blob)
  var aLink = document.createElement('a')
  aLink.style.display = 'none'
  aLink.href = url
  aLink.setAttribute('download', name)
  document.body.appendChild(aLink)
  aLink.click()
  document.body.removeChild(aLink) //下載完成移除元素
  window.URL.revokeObjectURL(url) //釋放掉blob對象
}

//下載文件
export function downloadExcel(data, name = 'file.xls') {
  if (!data) {
    return
  }
  var blob = new Blob([data], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
  })
  if (isIE()) {
    window.navigator.msSaveOrOpenBlob(blob, name)
    return
  }
  var url = window.URL.createObjectURL(blob)
  var aLink = document.createElement('a')
  aLink.style.display = 'none'
  aLink.href = url
  aLink.setAttribute('download', name)
  document.body.appendChild(aLink)
  aLink.click()
  document.body.removeChild(aLink) //下載完成移除元素
  window.URL.revokeObjectURL(url) //釋放掉blob對象
}

//判斷瀏覽器類型
function isIE() {
  if (!!window.ActiveXObject || 'ActiveXObject' in window) return true
  else return false
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章