My app suppose to download file from DB.
Controller:
@GetMapping(value = "/download/{id}")
public ResponseEntity<Resource> downloadBook(@PathVariable Long id) {
Book book = bookService.findById(id);
return ResponseEntity.ok().contentType(MediaType.APPLICATION_PDF)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + book.getFileName() + ".pdf\"")
.body(new ByteArrayResource(book.getFile()));
}
This works when i call it directly from browser (download pop up), but what i'm tring to do is handle it with ajax call. This is my ajax method so far, but this code actually just alert me with success with no dowload pop up.
downloadBook : function(bookId) {
$.ajax({
url : basePath + '/book/download/' + bookId,
success : function() {
alert("success!");
},
error : function() {
alert("error!");
}
});
}