Vue + Axios Excel requests downloaded in the background ResponseEntity

Back-end code

  /**
     * 小票数据导出
     */
    @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);
        
    }

Front-end code

. Axios that $ ({ // transmission axios post request 
        Method: ' POST ' ,
         // request address 
        URL: ' /export.do ' , 
         // Parameter 
        Data: { 
          orgId:. 1 
        }, 
        // show returned back to the server data type 
        responseType: 'BLOB' 
      }) 
      .then ((RES) => { // process the file stream returned 
          Debugger;
           const Content = res.data
           const BLOB = new 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)
          }
      })

 

Guess you like

Origin www.cnblogs.com/eason-d/p/11578315.html