Please I am annoyed with a problem. I need to download a xls created on the fly on the Java.
This is my client-side code:
exportToExcel: function(filters, exportData) {
$.ajax
({
type: "POST",
url: 'status/exportToExcel',
async: true,
data: {columns: exportData, filters: JSON.stringify(filters)},
success: function (data) {
var blob = new Blob([data], { "type" : "application/vnd.ms-excel" });
var objectUrl = URL.createObjectURL(blob);
window.open(URL.createObjectURL(objectUrl));
},
error: function (a,b,c){
alert(c);
}
});
}
And this is my serve-side code:
@ResponseBody
@RequestMapping(value = "exportToExcel")
public void exportToExcel(
@RequestParam(value = "columns", required = true) List<String> columns,
@RequestParam(value = "filters", required = false) String filters, HttpServletRequest request){
request.getHeaders().setContentType(getMediaType(excelFile));
request.getHeaders().set("Content-Disposition", String.format("attachment; filename=%s.%s", excelFile.getName(), excelFile.getFormat()));
try {
FileCopyUtils.copy(new DataInputStream(new FileInputStream(excelFile.getFile())), httpOutputMessage.getBody());
} catch (IOException e) {
logger.error("Exception in file download", e);
}catch(Exception e){
logger.error("Exception in file download", e);
}
}
I don't know how to show the download dialog, if it was GET a simple window.location solve the problem but my parameters can be so big that GET could not resolve.