I have an Ajax method that calls the controller to generate a file and get its URL:
$('.export-excel').on('click', function() {
var url = 'orders/excel';
$.get(url).success(function(data) {
console.log(data);
downloadFile("files/" + data);
}).error(function(data) {
console.log(data);
});
});
And here is the controller code:
@RequestMapping(value="/excel", method=RequestMethod.GET)
@ResponseBody
public String getExcel() {
List<Order> exportOrders=new LinkedList<Order>();
try {
exportOrders=orderService.get(filter);
} catch (Exception e) {
e.printStackTrace();
}
File excel=excelService.export(exportOrders, filter.getCurrency());
FileSystemResource resource=new FileSystemResource(excel);
return resource==null?StringUtils.EMPTY:resource.getFilename();
}
This works fine and returns the URL of the file correctly, but when I download it. The browser can't download it and I get the error "Failed - No File" on Chrome.
The only solution to download this for me is to refresh the workspace in Eclipse, but yet I have to refresh it every time I create a new file (which is not practical).
EDIT: This question is different than sjonshon's Question since the code here applies the solutions found there but the idea is that the file doesn't exist until the workspace is refreshed.