Vueの+ Axios Excelの要求が背景ResponseEntityでダウンロード

バックエンドのコード

  / * * 
     *小票数据导出
     * / 
    @RequestMapping(値 = " /export.do " 公衆 ResponseEntity < バイト []> receiptRawExport(HttpServletRequestのリクエスト、
            @RequestBody FilterReceiptDto DTO)は、IOExceptionがスロー{ 
        //干了很多活
        HSSFWorkbookブックを = ExcelUtils.excel(newlist、RequestContextの)。
        ByteArrayOutputStream出力 = 新しいByteArrayOutputStream(); 
        workbook.write(出力)。
        返す 新しい ResponseEntityを< バイト []>(output.toByteArray()、ヘッダ、HttpStatus.CREATED)。
        
    }

フロントエンドコード

。$({ことAxios // 伝送axiosポスト要求 
        :メソッド' POST ' // 要求アドレス 
        URL:' /export.do ' // パラメータ
        のデータ:{ 
          ORGID :. 1 
        }、
        // ショーが戻ってサーバーに返さデータ型
        responseType: 'BLOB' 
      })
      .then((RES) => { // ファイルストリームが返されるプロセス
          デバッガ;
           constのコンテンツ= res.data
           CONST BLOB = 新しい新しいブロブ([コンテンツ])
           CONSTファイル名=' 导出の.xls ' 
          であれば' ダウンロード'  のdocument.createElement(' A ')){ // 非IE下载
            CONST ELINK =のdocument.createElement(' A ' 
            elink.download = fileNameには
            elink.style.display = ' なし
            elink.href = URL.createObjectURL(ブロブ)
            document.body.appendChild(ELINK)
            elink.click()
            URL.revokeObjectURL(elink.href)// 释放URL对象
            document.body.removeChild(ELINK)
          }  { // IE10 +下载
            navigator.msSaveBlob(BLOBファイル名)
          } 
      })

 

おすすめ

転載: www.cnblogs.com/eason-d/p/11578315.html