js下載文件流,提示文件損壞問題


js下載文件流,提示文件損壞問題

前端axios,也需要加responseType: “blob”,這樣才能正常下載文件流。

eg:

axios({
    url: layui.setter.apiHost + '/api/order/putAllPages',
    method: "POST",
    headers: {
      Authorization: "Bearer " + layui.storage.getToken(),
      AppKey: layui.setter.TenantKey,
      TenantKey: layui.setter.TenantKey
    },
    timeout: 50000,
    data: Qs.stringify({
      type: _this.type,
      userType: 'buyer',
      querySubOrganization: subOrgan.querySubOrganization,
      organId: subOrgan.organId,
      startTime: startTime,
      endTime: endTime
    }),
    responseType: 'blob'
  }).then(function (res) {
    layer.closeAll('loading');
    var blob = new Blob([res.data], { type: "application/vnd.ms-excel" })
    var url = URL.createObjectURL(blob);
    var a = document.createElement("a");
    var fileName = '訂單核算.xls'
    a.download = fileName;
    a.style.display = "none"
    a.href = url;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    URL.revokeObjectURL(url)
  })


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM