antd-pro實現下載功能

在antd-pro中實現下載功能, 前端頁面可以這樣寫

<Button onClick={this.onDownLoadClick}> 下載</Button>
  onDownLoadClick = () => {
    const fileName = "下載文件.txt";
    const { dispatch } = this.props;
    dispatch({
      type: 'list/download',
      payload: {
        "id": 64,
      },
      callback: (response) => {
        console.log(response)
        // if (response.success) {
        const blob = new Blob([response]);
        const aLink = document.createElement('a');
        aLink.style.display = 'none';
        aLink.href = blob;
        aLink.download = fileName;
        document.body.appendChild(aLink);
        aLink.click();
        document.body.removeChild(aLink);
        // }
      }
    });
  }

但是我下載下來的文件內容並不是後臺需要的內容 ,
在這裏插入圖片描述
我感覺應該是後臺代碼的問題
後來後臺人員直接把下載的請求改成get方式,
前端只需要

window.open('/url/download?id=1')

就可以實現下載 , 說實話 不知道後臺幹了啥

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章