Spring - download response as a file

前端 未结 7 730
無奈伤痛
無奈伤痛 2020-12-08 07:19

I am writing application using AngularJS and Spring. I would like to send request to the server and download response returned from controller as a file. In controller I hav

7条回答
  •  情深已故
    2020-12-08 07:39

    //JAVA PART

    @RequestMapping(value = "/report-excel", method = RequestMethod.GET)
        public ResponseEntity getReportExcel(@RequestParam("bookingStatusType") String bookingStatusType,
                @RequestParam("endDate") String endDate, @RequestParam("product") String product, @RequestParam("startDate") String startDate)throws IOException, ParseException {
    
    //Generate Excel from DTO using any logic after that do the following
    byte[] body = wb.getBytes();
    HttpHeaders header = new HttpHeaders();
            header.setContentType(new MediaType("application", "xlsx"));
            header.set("Content-Disposition", "inline; filename=" + fileName);
            header.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            header.setContentLength(body.length);
    
     return new ResponseEntity(body, header, HttpStatus.OK);
    }
    
    
    
    //HTML PART
    
    
    Test
    
    
    
      

提交回复
热议问题