1 Reply Latest reply on Jul 14, 2009 8:07 AM by Busssard Buss

    File Downloading is not working for me

    Manish Prajapati Newbie
      Hi All,
      I want to create file downloading functionality for that i have written following code.It shows me the dialogue but if i open or save and open it gives me "format error:not a pdf or corrupted".

      Following is my code written to achieve the file downloading goal.

      public void download(){
                
                FacesContext facesContext = FacesContext.getCurrentInstance();
                HttpServletResponse response = (HttpServletResponse) facesContext
                            .getExternalContext().getResponse();
                Document document=new Document();
                //HttpServletResponse response = (HttpServletResponse)extCtx.getResponse();
                response.setContentType("application/pdf");
                      response.addHeader("Content-disposition", "attachment; filename=\"CoverPage.pdf\"");
                try {
                     ServletOutputStream serveltOutputStream = response.getOutputStream();
                     PdfWriter pdfWriter=PdfWriter.getInstance(document,serveltOutputStream);
                     document.open();
                     document.add(new Paragraph("A picture of my dog: otsoe.jpg"));
                     document.close();
                     pdfWriter.close();
                     serveltOutputStream.flush();
                     serveltOutputStream.close();
                     FacesContext.getCurrentInstance().responseComplete();     
                } catch(Exception e) {
                     //log.error("\nFailure : " + e.toString() + "\n");
                }
                //return null;
           }


      Can anybody tell me what's wrong with my code or how i can achieve the goal of file downloading?

      Thanks a lot in advance.

      Regards,
      Manish