0

Basically what I want to do is this : http://www.mkyong.com/java/how-to-export-data-to-csv-file-java/

Except at the end serve it as a download to the user. Obviously I can't use FileWriter as I'm using JSF,but I tried doing something similar by getting the output stream from the external context and then making a buffered writer using the output stream(code below).According to my logs this does execute completely till the end,but no save as dialog pops up.I am using JSF 2(MyFaces) and RichFaces 4.3,if there is a better way to do this using a component please tell me.

externalContext.responseReset();
externalContext.setResponseHeader("Content-Disposition", "attachment; filename=test.csv");
externalContext.setResponseContentType("text/csv");

try {
    OutputStream csvOut = externalContext.getResponseOutputStream();    
    BufferedWriter csvWriter = new BufferedWriter(new OutputStreamWriter(csvOut, "UTF-8"));

    csvWriter.append("data,data,data,data");

    csvWriter.flush();
    csvWriter.close();
    csvOut.close();

    facesContext.responseComplete();
}catch(Exception ex){
    System.out.println(ex.toString());
}

Any suggestions will help alot,been struggling with this for some time now.

Vinc
  • 695
  • 2
  • 10
  • 26
  • 1
    Try BalusC's kickoff example: http://stackoverflow.com/questions/2914025/forcing-a-save-as-dialogue-from-any-web-browser-from-jsf-application – uzvar Apr 26 '13 at 10:08
  • Thanks alot for the comment,can't believe I missed that question.Anyway that exact solution didn't work,but it helped me in the right direction.I have no idea how to upvote the comment/+rep in some way,but thanks.Will post what I did as answer as soon as I'm allowed. – Vinc Apr 26 '13 at 11:28

1 Answers1

1

Firstly thanks to uzvar for pointing me to Forcing a save as dialogue from any web browser from JSF application It's what helped me get mine working.

Basically all I did was instead of an OutputStream I declared an BufferedOutputStream with the externalContext.getResponseOutputStream(); as argument.So it looks like :

BufferedOutputStream csvOut = new BufferedOutputStream(externalContext.getResponseOutputStream());  

The rest of the code stays the same.

If there is a better way to do this please post an answer/comment.

Community
  • 1
  • 1
Vinc
  • 695
  • 2
  • 10
  • 26