blob文件下載記錄

export function downloadBlob(
  data,
  name,
  type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
) {
  if (name) {
    if (name.lastIndexOf(".") < 0) {
      name += ".xlsx";
    }
  } else {
    name += ".xlsx";
  }
  let blob = new Blob([data], { type });
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, name);
  } else {
    let objectUrl = URL.createObjectURL(blob);
    var a = document.createElement("a");
    a.download = name;
    a.href = objectUrl;
    a.click();
    document.body.appendChild(a);

    var evt = document.createEvent("MouseEvents");
    evt.initEvent("click", false, false);
    a.dispatchEvent(evt);
    document.body.removeChild(a);
  }
}

/**
 * 導(dǎo)出文件下載
 * @param  {[type]} res 后臺(tái)返回response
 * @return {[type]}     [description]
 */
export function exportDownload(that,res){
  if (res.data.type == 'application/json') {
      // 將blob文件流轉(zhuǎn)換成json
      const reader = new FileReader();
      reader.readAsText(res.data);
      reader.onload = function (event) {
        const message = JSON.parse(event.target.result).message;
        that.$message.error(message);
      }
      return false;
    }
    const blob = new Blob([res.data]);
    let str = res.headers['content-disposition'];
    let filename = decodeURI(str.substr(str.indexOf('=') + 1));
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        //ie使用的下載方式
        window.navigator.msSaveOrOpenBlob(blob, filename);
     } else {
        let elink = document.createElement("a");
        // 設(shè)置下載文件名
        elink.download = filename;
        elink.style.display = "none";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
    }
}


// 下載xls
export function downLoadFile (data, name) {
let a = document.createElement('a')
a.download = `${name}.xls`;
a.style.display = 'none'
//獲取請(qǐng)求返回的response對(duì)象中的blob 設(shè)置文件類(lèi)型,這里以excel為例
let blob = new Blob([data], { type: 'application/[vnd.ms](http://vnd.ms/)-excel' })
//創(chuàng)建一個(gè)臨時(shí)的url指向blob對(duì)象,創(chuàng)建url之后可以模擬對(duì)此文件對(duì)象的一系列操作,例如:預(yù)覽、下載
a.href = URL.createObjectURL(blob)
document.body.appendChild(a);
a.click()
document.body.removeChild(a);
}

/**
 * 下載流文件文件工廠函數(shù)
 * @param {*} param0 
 * @returns 
 */
export function axiosDownloadFactory({ url, params }) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url,
      params,
      responseType: 'blob'
    }).then((response) => {
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement('a');
      const fileName = response.headers['content-disposition'].match(/=(.*)$/)[1]; // 此方法為獲取后臺(tái)文件名,也可以當(dāng)作參數(shù)傳遞進(jìn)來(lái)
      link.href = url;
      link.setAttribute('download', decodeURI(fileName));
      document.body.appendChild(link);
      link.click();
      resolve();
    }).catch((error) => {
      reject(error)
    })
  })
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容