I have a java servlet called DownloadFile and in the Get Method I have some code that gets a file from a database and downloads it when I navigate to the url
localhost:9080/myapp/DownloadFile
But I dont want to send my user to this page to have to download the file, so in my javascript on the click of a button I have this
$scope.downloadTemplate = function(){
console.log("download template called");
$.get("DownloadTemplate", function(responseText) {
console.log(responseText)
});
}
The function gets called, and the servlet gets called but the file does not download. Why? Here is relevant servlet code
bytes = templateSet.getBytes("FILE");
fileName = templateSet.getString("TEMPLATE_FILE_NAME");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=" + fileName);
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.flush();
out.close();
How can I download the file and keep the user on the page where the download button is?
Thanks