You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by CarlHowarth <ca...@dlapiper.com> on 2006/10/03 13:01:15 UTC

commandLink commandButton differences in file download behaviour problem

Hello there,

Hopefully someone can help - I have looked through the existing queries and
can't just find what I'm after. 

I have a main record with asociated files - the files are uploaded through
my web app and are downloaded from the same page that lists the files in a
dataTable. I have a series of commandLinks that display the file names - a
user selects the file and it downloads. 

The problem I have is that when a user downloads a file then immediately
trys to upload another, they are prompted to download the file that they
have already previously downloaded. If however I replace the commandLinks
for commandButtons the application behaves correctly.

I have seen an associated link that mentions similar behaviour
(http://blogs.sun.com/tor/entry/creating_downloadable_files), however I do
not want to use the command buttons in this manner, and I use an
actionListener rather than an action so I may retrieve information from the
list of dynamically created files. I am not receiving any errors, just the
strange behaviour with commandLinks.

My code is as follows:

 public void downloadFile(ActionEvent ev) {
        UIData fileList = findParentHtmlDataTable(ev.getComponent());
        AssociatedFile associatedFile = (AssociatedFile)
fileList.getRowData();
        String downloadPath = "download path retrieved here...";

        try {
            byte[] fileData = InternalUtil.readFile(downloadPath);
            HttpServletResponse response =
                    (HttpServletResponse)
getFacesContext().getExternalContext().getResponse();

            response.setHeader("Content-disposition", "attachment;
filename=\"" +
                    associatedFile.getName() +
                    "\"");


            response.setContentLength(fileData.length);
            response.setContentType(associatedFile.getContentType());
            ServletOutputStream out = response.getOutputStream();
            out.write(fileData);

           
getFacesContext().getApplication().getStateManager().saveSerializedView(getFacesContext());
            getFacesContext().responseComplete();


        } catch (FileSystemException e) {
            getFacesContext().addMessage("null", new FacesMessage("Error
downloading file: " +
                    e.getMessage()));
        } catch (IOException e) {
            getFacesContext().addMessage("null", new FacesMessage("Error
downloading file: " +
                    e.getMessage()));
        }
}

Any help would be greatly appreciated!

Thanks in anticipation, Carl    

-- 
View this message in context: http://www.nabble.com/commandLink-commandButton-differences-in-file-download-behaviour-problem-tf2375460.html#a6617934
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: commandLink commandButton differences in file download behaviour problem

Posted by Carl Howarth <ca...@dlapiper.com>.
Right - I have a work around. It's a bit cumbersome but works well and avoids
the need to create a custom export servlet. It involves Rest Faces so I will
go through what I have done to hopefully let others avoid the pain and
frustration!

- First I added the restfaces libs to my project (see
https://restfaces.dev.java.net/ for further info).
- Add a new XML to the WEB-INF dir called rest-faces-rules.xml. Mine has the
following content:

<?xml version="1.0" encoding="UTF-8"?>

<rest-faces>

  <action name="exportpo" value="#{exportBean.downloadPO}">
    <property param="poid" value="#{exportBean.purchaseOrderId}"/>
    <navigation-case>
      <from-outcome>*</from-outcome>
      <to-view-id>/maintainpurchaseorder.jsp</to-view-id>
    </navigation-case>
  </action>

</rest-faces>

- I added a new Export Bean with request scope and moved my downloadPO
method there as there will be other downloads required within the app. 

- I added the rest faces taglib to the calling page:

<%@ taglib uri="http://restfaces.dev.java.net" prefix="rest" %>

- I replaced the troublesome commandlink with the following:

<rest:link value="/exportpo.jsf">
  <h:outputText value="Download PDF"/>
  <f:param value="#{maintainPurchaseOrderBean.purchaseOrder.id}"
name="poid"/>
</rest:link>

- Note that the Export bean did not need to have the purchase order id
defined as a managed property in the faces-config.xml as the rest-faces
config must have done this wiring for me. There is no additional config
required to get rest-faces up and running. 

This is my first attempt with rest faces, and although it may not be the
most elegant way of achieving my goal, it works.

If anybody does have a solution to make the standard commandlink work I
would still be very happy to hear it!

Cheers, Carl

-- 
View this message in context: http://www.nabble.com/commandLink-commandButton-differences-in-file-download-behaviour-problem-tf2375460.html#a13874507
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: commandLink commandButton differences in file download behaviour problem

Posted by Carl Howarth <ca...@dlapiper.com>.
Hello again,

It's now over a year on and I've come back to this problem in a project that
I am working on. I am now creating a PDF, and am still having issues with
Internet Explorer 6. If I use a command link the pdf downloads successfully,
but then any subsequent command link I select prompts me to download the
file again. I have trawled the web in order to find an answer but am still
stumped. As before the download works without an issue in Firefox.

I am now using MyFaces 1.2.0, Tomahawk 1.1.6 and also have Richfaces 3.1.2.
I am running on Tomcat 6.0.14.

Here is my commandlink:

<t:commandLink value="Download PDF" rendered="#{!
maintainPurchaseOrderBean.editPOItemsMode}"
actionListener="#{maintainPurchaseOrderBean.downloadPO}" />

Here is my code, I have added the various cache control headers in an
attempt to fix the problem but to no avail:

  public void downloadPO(ActionEvent ev) {
        _log.debug("Downloading PO");
        FacesContext fc = FacesContext.getCurrentInstance();
        try {
            HttpServletRequest req = (HttpServletRequest)
fc.getExternalContext().getRequest();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            HttpServletResponse response = (HttpServletResponse)
fc.getExternalContext().getResponse();
            StateManager stateManager = (StateManager)
fc.getApplication().getStateManager();

            _purchaseOrderService.generatePurchaseOrderDownload(baos,
_purchaseOrder);

            response.setHeader("Content-disposition", "attachment;
filename=\"" + _purchaseOrder.getId() + ".pdf\"");
            response.setContentType("application/pdf");
            response.setContentLength(baos.size());
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Cache-Control", "max-age=0");
            response.setHeader("Pragma", "no-cache");

            baos.writeTo(response.getOutputStream());

            response.getOutputStream().flush();
            response.getOutputStream().close();
            response.flushBuffer();

            stateManager.saveSerializedView(fc);
            fc.responseComplete();
        } catch (IOException e) {
            fc.addMessage(null, new
FacesMessage(FacesMessage.SEVERITY_ERROR, "Error downloading PDF", null));
            _log.error(e);
        }
    }

Has anybody got a solution for this issue? I look forward to hearing one!

Many thanks, Carl

-- 
View this message in context: http://www.nabble.com/commandLink-commandButton-differences-in-file-download-behaviour-problem-tf2375460.html#a13858353
Sent from the MyFaces - Users mailing list archive at Nabble.com.