Download CSV file via Rest
csv, java, opencsv, rest
Solution
To force "save as", you need to set the content disposition HTTP header in the response. It should look like this:
Content-Disposition: attachment; filename="whatever.csv"
It looks like you're using JAX-RS. This question shows how to set the header. You can either write the CSV to the HTTP response stream and set the header there or return a `Response` object like so:
return Response.ok(myCsvText).header("Content-Disposition", "attachment; filename=" + fileName).build();
You do not need to write to a `File` object in the middle of this process so can avoid writing to disk.
Problem
Using OpenCSV, I can successfully create a CSV file on disc, but what I really need is to allow users download the CSV with a download button, I don't need to save on disk, just download. Any ideas? ``` @GET @Path("/downloadCsv") public Object downloadCsv() { CSVWriter writer; FileWriter wr; //Or should I use outputstream here? wr= new FileWriter("MyFile.csv"); writer = new CSVWriter(wr,','); for (Asset elem: assets) { writer.writeNext(elem.toStringArray()); } writer.close(); } ``` EDIT: I do NOT want to save/read file on disc EVER