0

I have this button on my html page. When I click it, exportCOAExcel gets triggered. Then a excel workbook is generated and save to a path.

I want a prompt to come to say which path you want to download the file to? or save to the deafult 'download' folder location of the browser.

@GetMapping(value = "/coaExport", params = "action=excel")
public void exportCOAExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    //loogic to fill up the excel workbook with data

    FileOutputStream outputStream = new 
    FileOutputStream("C:\\Users\\user\\Desktop\\revenue.xls");
    workbook.write(outputStream);
    outputStream.close();

}
Jacel
  • 307
  • 4
  • 10
  • 24
  • I assume that your web application is running on a server - there's no point in saving the file to a particular location on the server - instead you need to send the file to the HttpServletResponse and set the right headers. You don't get to choose where the file gets saved - that's up to the user's browser. – Erwin Bolwidt May 27 '18 at 10:18
  • How would you send the file to the response? – Jacel May 27 '18 at 11:44
  • Possible duplicate of [Spring boot service to download a file](https://stackoverflow.com/questions/28407215/spring-boot-service-to-download-a-file) – Robin Green May 27 '18 at 13:18

1 Answers1

0

You need to send a content disposition response to the browser, so that it can understand that it is a file to download.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

Dibsyhex
  • 119
  • 7