vue axios get和post請求下載文件,后台springmvc完整代碼


 注意請求時要設置responseType,不加會中文亂碼,被這個坑困擾了大半天。。。
axios post請求:
    download(index,row){
        var ts = this;
        axios.post(this.paths.baseURL+'file/downloadFile',
        {path:row.zurl},
        {responseType: 'blob'}
        ).then(msg => {
          console.log(msg.data)
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = window.URL.createObjectURL(new Blob([msg.data]));
          link.setAttribute("download",row.zurl.substring(row.zurl.lastIndexOf("/")+1));//完整文件名稱
          document.body.appendChild(link);
          link.click();
          URL.revokeObjectURL(link.href);
          document.body.removeChild(link);
        }).catch(error =>{
          ts.$message.error('下載文件失敗')
        })
      }

 

后台接口:
 
          
    @RequestMapping(value = "downloadFile",method = RequestMethod.POST,produces = {"application/json;charset = utf-8"})
    @ApiOperation(notes = "下載",value = "下載")
//    @RequiresPermissions("file:download")
    public ResponseEntity<byte[]> downloadFile(@RequestBody Map<String,Object> map) throws IOException {
        String path = map.get("path").toString();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment",path.substring(path.lastIndexOf("/")+1));
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(path)),headers, HttpStatus.CREATED);
}

axios get請求下載
        axios.get(this.paths.baseURL+'risk/excelRisk',
        {responseType: 'blob'}
        ).then((msg) => {
          console.log(msg)
              let url = window.URL.createObjectURL(new Blob([msg.data]));
              let link = document.createElement('a')
              link.style.display = 'none'
              link.href = url
              link.setAttribute('download', '未評估風險數據.xls')
              document.body.appendChild(link)
              link.click()              
          }).catch((error) => {
            console.log(error)
              this.$message({
                type:'error',
                message:'服務器異常'                 
              })
          })
    @GetMapping(value = "excelRisk",produces = {"application/octet-stream;charset=utf-8"})
    //@RequiresRoles("risk:list")
    public void excelRisk(HttpServletResponse response) {
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            Workbook workbook = RiskExcel.wpgRisk(riskService.excelRisk(map),"excelTemplate/risk_1.xlsx");
            OutputStream out=response.getOutputStream();
            response.setHeader("Content-Type","application/vnd.ms-excel");
            response.addHeader("Content-Disposition","attachment;filename=為評估數據.xmls");
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM