Thanks for responding Aris,

I have implemented what you suggested and am still getting the same problem
(note I am using MyFaces 1.0.7). Have I implemented it below the way you
have it?  I put an actionListener on my commandLink and use an ActionEvent
method to capture the line of the dataTable that they selected. I then call
writeFileToBrowser and at the end specify
FacesContext.getCurrentInstance().responseComplete().

Do you see where I may have something wrong?

<h:commandLink  
        actionListener="#{DocumentCtl.downloadPDF}" 
        immediate="true">
  <h:outputText value="view" styleClass="copy"/>
</h:commandLink>        

    public void downloadPDF(ActionEvent event) {
        Document doc = (Document) UIHelper.getReqMapObj("document");
        String file = Sysctl.getRecord().getLowresfolder() + doc.getGuid() +
".pdf";
        writeFileToBrowser(file, doc.getName() + ".pdf",
doc.getUploadtype());
    }

    private void writeFileToBrowser(String fileSource, String downloadName,
String contentType) {

        if (!FacesContext.getCurrentInstance().getResponseComplete()
                && FileControl.exists(fileSource)) {

            HttpServletResponse response = (HttpServletResponse)
FacesContext.getCurrentInstance()
                    .getExternalContext().getResponse();
            response.setContentType(contentType);
            response.setHeader("Content-Disposition",
"attachment;filename=\"" + downloadName
                    + "\"");

            try {
                ServletOutputStream out = response.getOutputStream();
                BufferedInputStream bufferedinputstream = new
BufferedInputStream(
                        new FileInputStream(fileSource));

                byte abyte0[] = new byte[4096];
                int i;
                while ((i = bufferedinputstream.read(abyte0, 0, 4096)) !=
-1)
                    out.write(abyte0, 0, i);

                out.flush();
                out.close();

                FacesContext.getCurrentInstance().responseComplete();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }




-----Original Message-----
From: Aris Bartee [mailto:[EMAIL PROTECTED] 
Sent: Saturday, April 23, 2005 9:05 AM
To: MyFaces Discussion
Subject: Re: Download file from browser problem - two clicks

I wrapped my download code in an event method:
foo(ActionEvent event).  The immediate attribute for
the uicommand tag should be true.
--- Aaron Bartell <[EMAIL PROTECTED]> wrote:
> Below is the problem I am still having that I am
> wondering if anyone has
> found a solution to.  Is there a way to make it so
> the user doesn't have to
> click on the same link twice after a file download
> has occurred?  Almost
> seems like something in the JSF lifecycle isn't
> completing correctly.  I am
> returning null from action method of the link
> clicked so I stay on the same
> page.  Any thoughts?
> 
>  
> 
> Thanks,
> 
> Aaron Bartell
> 
>  
> 
>  
> 
>  
> 
>  
> 
>  
> 
> Here is how I have done it based on things I have
> found on the net. The nice
> thing about streaming it to the browser is that the
> file need not exist in
> your context, it can be anywhere on the file system.
> The only bummer is that
> this seems to create a problem after the user
> downloads. Any button or link
> that they click after I do the .responseComplete()
> it doesn't work the first
> time. But the second time they click the button or
> link it executes the
> appropriate code. I am open to suggestions on any
> errors in my code to fix
> that problem...
> 
>  
> 
> 
> private void writeFileToBrowser(String fileSource,
> String downloadName,
> String contentType) {
> 
> if
>
(!FacesContext.getCurrentInstance().getResponseComplete()
> &&
> FileControl.exists(fileSource)) {
> 
> HttpServletResponse response = (HttpServletResponse)
> FacesContext.getCurrentInstance()
> .getExternalContext().getResponse();
> response.setContentType(contentType);
> response.setHeader("Content-Disposition",
> "attachment;filename=\"" +
> downloadName + "\"");
> 
> try {
> ServletOutputStream out =
> response.getOutputStream();
> BufferedInputStream bufferedinputstream = new
> BufferedInputStream(new
> FileInputStream(
> fileSource));
> 
> byte abyte0[] = new byte[4096];
> int i;
> while ((i = bufferedinputstream.read(abyte0, 0,
> 4096)) != -1)
> out.write(abyte0, 0, i);
> 
>                out.flush();
>                out.close();
> 
>  
> 
>               
>
FacesContext.getCurrentInstance().responseComplete();
>            } catch (IOException ex) {
>                ex.printStackTrace();
>            }
>        }
>    }
> 
>  
> 
>    public static boolean exists(String fileSource) {
>        File f = new File(fileSource);
>        return f.exists();
>    }
> 
>  
> 
> HTH,
> Aaron Bartell
> 
>  
> 
> 
> 
> 
> Patrick B Haggood wrote:
> 
>  
> 
> Anyone have an example of downloading using MyFaces?
>  I see on the
> sample you can use outputtext components, but that's
> using the real
> filename (which I keep in a datastore to guard
> against duplicate
> filenames; the system assigns new filename,
> FILE0002, FILE0003, etc
> apon upload)
> 
>  
> 
> Thanks!
> 
>  
> 
> 

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to