导出后端给的文档流

导出后端的文档流,如果不需要加token,直接a标签链接上就行。

需要加请求参数,写法如下:

  <Button
            style={{ float: 'right', marginTop: 6 }}
            onClick={()=>{
              fetch(`/api/v1/syncstatus/down?job_id=${id}`,{
                headers:{
                  'Authorization': getAccountToken(),
                  'content-type':'application/json'
               }
              }).then(res =>
                  res.blob().then(blob => {
                    const a = document.createElement('a');
                    document.body.appendChild(a);
                    const exportUrl = window.URL.createObjectURL(blob);
                    const filename = `${new Date().getTime()}.xlsx`;
                    a.href = exportUrl;
                    a.download = filename;
                    a.click();
                    window.URL.revokeObjectURL(exportUrl);

// const blob = new Blob([res], {type: "img/jpeg"});
// const imgUrl = window.URL.createObjectURL(blob)
// const downloadElement = document.createElement("a")
// downloadElement.style.display = "none";
// downloadElement.href = imgUrl
// downloadElement.download = `${payload.key_name}`
// document.body.appendChild(downloadElement)
// downloadElement.click()
// document.body.removeChild(downloadElement)
// window.URL.revokeObjectURL(imgUrl)


                  }),
                )

             // window.location.href=`/api/v1/syncstatus/down?job_id=${id}`
            }
            }
          >
            全部导出
          </Button>

 

posted @ 2023-02-23 10:43  荷风伊夏  阅读(26)  评论(0编辑  收藏  举报