I'm using the apache.commons.csv library in Java to generate CSV and make it downloadable without storing on server using following code:
try (OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream())){
csvWriter = new CSVPrinter(osw,CSVFormat.DEFAULT);
writer.printRecord(row);
response.setContentType("text/csv");
response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\";");
catch(Exception e) {
//Exception block
}
finally {
try {
csvWriter.flush();
} catch (IOException e) {
logger.error("Exception while flushing file writer and CSV writer",e);
}
}
But it showing csv on browser, rather than asking me save as option for download.
What is wrong in the code or is there any other way to do the same?