【筆記】下載文件

頁面部分:

//點擊某個按鈕下載
toDownLoad () {
      downloadApi({'參數'}).then(res => {
        let url = window.URL.createObjectURL(new Blob([res], {type: "application/vnd.ms-excel;charset=utf-8"}))
        let link = document.createElement('a')    //創建a標籤
        link.style.display = 'none'   //隱藏a標籤節點
        link.href = url     //a標籤的路徑
        link.setAttribute('download', '申請任務.xls')  // 自定義下載文件名(如exemple.txt)
        //以上內容其實是生成一個a標籤,如:<a download="文件名" href="文件下載接口地址"></a>
        document.body.appendChild(link)    //將a標籤加入節點
        link.click()   //觸發點擊
      })
    },

封裝api部分:

export const downloadApi= () => {
  return axios.request({
    url: '請求路徑',
    responseType: 'blob',
    method: 'post'
  })
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章