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)
  })
posted @ 2021-08-24 17:10  zc-lee  阅读(2603)  评论(0编辑  收藏  举报