Downloading CSV file at client machine

Discussions

Web tier: servlets, JSP, Web frameworks: Downloading CSV file at client machine

  1. Downloading CSV file at client machine (5 messages)

    Hi All
     I am trying to generate a CSV report so that after generation, the client would be able to download the report to his/her machine. The jsp looks like this:

        byte[] outBytes = myBean.getCSVBytes();
        response.setContentType ("application/x-msexcel");
        response.setHeader ("Content-Disposition", "attachment;filename=\"ComputeOrder.csv\"");

        byte[] buf = outBytes;
        int length = 0;
        ServletOutputStream op = response.getOutputStream ();
        op.write(buf,0,length);

    Now the problem is that when the page is loaded, although the dialogue box is thrown for "Save/Run" application, when i save it to the local machine and open it, i dont see anything. Yes absolute blank. But if I try to print out the bytes in console, i do see output(report content).
    What might be wrong?


    Thanks
    George
  2. Downloading CSV file at client machine[ Go to top ]

    becuase your length variable is set to 0 and then never changed before your call to op.write(buf,0,length); That calls op.write(buf,0,0); which writes 0 bytes.
  3. Downloading CSV file at client machine[ Go to top ]

    Thanks a lot Dave. Also another question... how can I close the window(page on which the download took place)after the download??

    Thanks in advance
  4. Downloading CSV file at client machine[ Go to top ]

    Additional tip:

    call resp.setContentLength
  5. Downloading CSV file at client machine[ Go to top ]

    Another option...


    Use

     response.setHeader ("Content-Disposition",
         "inline;filename=\"ComputeOrder.csv\"");

    instead of

     response.setHeader ("Content-Disposition",
         "attachment;filename=\"ComputeOrder.csv\"");
  6. Downloading CSV file at client machine[ Go to top ]

    Thanks a lot