2 Replies Latest reply on Sep 2, 2009 7:55 AM by Žiga Gantar

    a4j:commandLink forcing download

    Žiga Gantar Newbie

      Hi all,

      I am having trouble setting a4j:commandLink to force browser to download a file. Link is invoking a method that writes to ServerOutputStream some random file, multiple file types, but the browser is rendering what it gets instead poping up save dialog. Below is the method in backing bean and the a4j:commandLink on the page.

       if (tmpContractAttachment != null) {
       HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
       .getExternalContext().getResponse();
       response.setContentType("octet-stream");
       response.setHeader("Content-Disposition","attachment; filename=\"" +tmpContractAttachment.getFileName() + "\"");
       response.setHeader("Pragma", "no-cache");
       response.setHeader("Expires", "0");
       response.setContentLength(tmpContractAttachment.getSize());
       try {
       ServletOutputStream ouputStream = response.getOutputStream();
       ouputStream.write(tmpContractAttachment.getFile());
       ouputStream.flush();
       ouputStream.close();
       } catch (IOException e) {
       log.error("IOexception");
       }
       FacesContext.getCurrentInstance().responseComplete();
       }
      


      <h:commandLink value="#{attachment.fileName}" action="#{XXX.getFile(attachment.fileName)}"/>
      


      The funny thing in all this is that for some reason h:commandLink works - sometimes. Sometimes it works but sometimes firebug logs an error
      f is undefined
      In that case reloading the page helps.