i have that scenario that i am building a menu programmatically and adding a menu item that is supposed to download a file, so i googled a way to add a af:fileDownloadActionListener to a command menu item but i could not find, so what i reached to is adding an action (or action listener) that calls on the method of the af:fileDownloadActionListener (public void downloadUserGuide(FacesContext context, OutputStream outputStream)) that downloads the file, so i had to pass on a FacesContext Object and OutputStream object to that method, the download works and i am able to download my file but the problem is that after i download my file, my ADF Application freezes and does not respond at all, below the code shows what i did
| |
| FacesContext fc = FacesContext.getCurrentInstance(); |
| ExternalContext ec = fc.getExternalContext(); |
| ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide. |
| ec.setResponseContentType("application/pdf"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ExternalContext#getMimeType() for auto-detection based on filename. |
// | ec.setResponseContentLength(9729203); // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown. |
| ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + "WorkOrders - User Guide.pdf" + "\""); // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead. |
| try { |
| OutputStream output = ec.getResponseOutputStream(); |
| downloadUserGuide(FacesContext.getCurrentInstance(), output); |
| } catch (IOException e) { |
| System.out.println("IO EXception message "+e.getMessage()); |
| e.printStackTrace(); |
| } |
| // Now you can write the InputStream of the file to the above OutputStream the usual way. |
| // ... |
| |
| fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed. |
| |
| |
and the (public void downloadUserGuide(FacesContext context, OutputStream outputStream) is shown below
| public void downloadUserGuide(FacesContext context, OutputStream outputStream) { |
| System.out.println("in the download user guide method"); |
| DCIteratorBinding iterator = ADFUtils.findIterator("Applications1Iterator"); |
| Row currentRow = iterator.getCurrentRow(); |
| |
| ApplicationsVORowImpl newRow =(ApplicationsVORowImpl)currentRow; |
| |
| BlobDomain blobDomain = newRow.getUserGuide(); |
| // // System.out.println("current row index in range is : "+iterator.getCurrentRowIndexInRange()); |
//System.out.println("blobdomain is "+blobDomain);
| if(blobDomain!= null){ |
| |
| HttpServletResponse responce =(HttpServletResponse)context.getExternalContext().getResponse(); |
| responce.setContentType(newRow.getContentType()); |
| byte[] bytearray = new byte[8192]; |
| InputStream is; |
| try { |
| is = blobDomain.getBinaryStream(); |
| int byteread = 0; |
| while ((byteread = is.read(bytearray)) != -1) { |
| outputStream.write(bytearray, 0, byteread); |
| } |
| is.close(); |
| outputStream.flush(); |
| outputStream.close(); |
| |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
any help is appreciated, thanks