Axios 下载文件 / SpringBoot 传输文件


SpringBoot

private static void setResponseHeader(HttpServletResponse response, String fileName) {
    try {
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Axios

this.axios.get("/api/sc/download/1", {responseType: 'blob'}).then(res=>{
    let fileName = decodeURI(res.headers['content-disposition'].substring(20))
    let url = window.URL.createObjectURL(new Blob([res.data]))
    let link= document.createElement('a')
    link.style.display='none'
    link.href=url
    link.setAttribute('download', fileName)
    document.body.appendChild(link)
    link.click()
})

文章作者: ❤纱雾
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 ❤纱雾 !
评论
  目录