【笔记】下载文件

页面部分:

//点击某个按钮下载
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'
  })
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章