I have a Vue application in which I have an "downloadCsv" method. When a button is pressed, this method is invoked and the following rest GET call is made:
@GetMapping(value = "downloadCsv")
public ResponseEntity<Resource> exportSubmitted() throws IOException {
//Generates CSV file and returns fileName
String filePath = myService.generateCSV();
InputStreamResource resource = new InputStreamResource(new FileInputStream(filePath));
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "data.csv" + "\"")
.body(resource);
}
When I call this endpoint directly, i.e. localhost:8080/downloadCsv
in the browser(chrome), the csv is downloaded.
However, when I call this endpoint from within my Vue app on click, i.e.:
axios.get(endpoint)
.then(result => {
const blob = new Blob([result], {type: 'text/csv'});
FileSaver.saveAs(blob, 'download.csv');
});
I am unable to have the browser download the file.
What is the correct way to call a rest endpoint that returns a file and download it within the browser in Vue?