1 Reply Latest reply on Oct 8, 2008 8:56 AM by bm97

    a4j:commandLink+file downloading...

      hello ppl, as usual, i have a problem :)
      My application needs to allow the user to download files from the server.
      I used on my jspx:

      <a4j:commandLink
      action="#{versementsController.generateFile}"
      value="#{msg.generateFile}" />
      on versementsController the file is generated and then added to the response with:

      public static void attachFileToResponse(String fileName, String fileContent,String contentType) throws Exception{
      OutputStream out;
      HttpServletResponse response=getResponse();
      if( response != null ){

      response.setContentType(contentType);
      response.setHeader( "Content-Disposition",
      "attachment;filename="+fileName);
      response.setContentLength(fileContent.length());
      out = response.getOutputStream( );
      }
      else{
      out = new FileOutputStream( fileName );
      }
      try {
      out.write(fileContent.getBytes());
      }
      catch(Exception e ){
      handleException( e );
      }
      finally{
      try{
      out.close( );
      }
      catch( Exception e ){
      handleException( e );
      }
      finally{
      if( response != null ){
      // there cannot be HTTP multipart response. if the page needs refreshing, it MUST be done by another request
      FacesContext.getCurrentInstance().responseComplete();
      }
      }
      }
      }

      where contentType="application/force-download"

      The problem is that the response is shown in the browser window when the right behaviour is that the Save As... window should appear.

      I solved this problem by using h:commandLink, but i was wondering what could cause that behaviour?