0 Replies Latest reply on Oct 30, 2007 5:51 AM by andrei.dumitru

    Send attachment via HttpServletResponse using a4j:form

    andrei.dumitru


      Hi All,

      I have this problem trying to export data from a dataTable to a csv : the content of my HttpServletResponse is displayed on the screen in the browser instead of being send as an attachment, as the form that includes my dataTable and my export - button is a a4j:form (and the button an a4j:button). If i change the a4j:form to an h:form the problem is solved, it works like a charm, but i would like to keep form an a4j one.

      Here is the code for my export method:

      public static void export(String payload) {

      FacesContext ctx = FacesContext.getCurrentInstance();
      if (!ctx.getResponseComplete()) {


      HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();

      response.reset();

      URLConnection.guessContentTypeFromName("out.csv");

      response.setContentType(URLConnection.guessContentTypeFromName("out.csv"));

      response.setContentLength(payload.getBytes().length);

      response.setHeader("Content-Disposition","attachment; filename=\"" + "out.csv" + "\"");

      try {
      ServletOutputStream out = response.getOutputStream();
      logger.debug("Response string : " + payload);
      out.write(payload.getBytes());
      out.flush();
      }
      catch (IOException io )
      {
      logger.debug("Exception occured in method export!");
      logger.error(io.getMessage());
      }
      finally{
      try {
      response.getOutputStream().close();
      ctx.responseComplete();
      } catch (IOException e) {
      logger.debug(e.getStackTrace());
      }

      }
      }

      Has anyone encountered a simillar issue and a solving for this problem?


      Thanks in advance,
      Andrei