天天看點

javascript 下載下傳方法

  • javascript 下載下傳方法
import axios from '@/common/services/axios-instance'
function download(url, fname) {
  return axios({
    url,
    responseType: 'blob',
  }).then((res) => {
    if (flag) ProgressBar.done()
    if ('msSaveOrOpenBlob' in window.navigator) {
      // 相容IE
      window.navigator.msSaveOrOpenBlob(res.data, fname)
      return
    }
    const blobUrl = URL.createObjectURL(res.data)
    const a = document.createElement('a')
    a.href = blobUrl
    a.download = fname
    // 不用a.click()因為52的firefox不允許
    var evt = document.createEvent('MouseEvents')
    evt.initEvent('click', true, true)
    a.dispatchEvent(evt)
    URL.revokeObjectURL(blobUrl)
  })
}
export default download
           

繼續閱讀