Vue + Axios Excel 請求后台下載 ResponseEntity


后端代碼

  /**
     * 小票數據導出
     */
    @RequestMapping(value = "/export.do" )
    public ResponseEntity<byte[]> receiptRawExport(HttpServletRequest request,
            @RequestBody FilterReceiptDto dto) throws IOException {
        // 干了很多活
        HSSFWorkbook workbook = ExcelUtils.excel(newlist,requestContext);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        workbook.write(output);
        return new ResponseEntity<byte[]>(output.toByteArray(), headers, HttpStatus.CREATED);
        
    }

前端代碼

that.$axios({ // 用axios發送post請求
        method: 'post',
        // 請求地址
        url: '/export.do', 
        // 參數
        data: {
          orgId: 1
        }, 
        // 表明返回服務器返回的數據類型
        responseType: 'blob'
      })
      .then((res) => { // 處理返回的文件流
          debugger;
          const content = res.data
          const blob = new Blob([content])
          const fileName = '導出.xls'
          if ('download' in document.createElement('a')) { // 非IE下載
            const elink = document.createElement('a')
            elink.download = fileName
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href) // 釋放URL 對象
            document.body.removeChild(elink)
          } else { // IE10+下載
            navigator.msSaveBlob(blob, fileName)
          }
      })

 


免責聲明!

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



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