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!

 

Reply via email to