0

I am trying to make the user able to download a file

<h:commandLink value="yes" actionListener="#{Bean.readFileFromServer}"/>

the method redFileFromServer is a method that returns void, it does some logic then it calls anthoer method to return the file to the response

 public void exportList() throws IOException {
        FacesContext fc = FacesContext.getCurrentInstance();
        fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        ec.responseReset();
        try {
            userLogger.info("Exposting report ");

            ec.setResponseContentType("application/octet-stream"); // Check

            ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + result.getFileName() + "\"");
            OutputStream output = ec.getResponseOutputStream();

            int octet;

            while ((octet = input.read()) != -1) {
                output.write(octet);
            }

            input.close();
            output.close();

            fc.responseComplete();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

the file downloaded is compressed zip file, I have 2 problems, first sometimes this works and sometimes not, and I don't know why the behavior is not consistence.Second problem if there is any exception the page is redirected, which is unacceptable duo to my structure design, I need this to work every time and in case of any problem the page won't redirect.

Aritz
  • 30,971
  • 16
  • 136
  • 217
Yara Mousa
  • 79
  • 6
  • 15
  • Don't think that's posible with HTTP. Redirection headers are different from download ones, so I'm afraid you won't be able to perform it in the same request. Your best is to do it the other way, redirect and download the file later on: http://stackoverflow.com/questions/822707/php-generate-file-for-download-then-redirect – Aritz Aug 25 '14 at 11:38
  • @Xtreme: the title was badly formulated. As far as I understood the question, OP just do **not** want the thing to cause a "redirect" (to error page) in case of an exception. – BalusC Aug 25 '14 at 11:46
  • @BalusC what I want is to redirect in case of exception – Yara Mousa Aug 26 '14 at 10:09
  • You literally said that this is "unacceptable"? – BalusC Aug 26 '14 at 10:11
  • I am sorry I got confused, you are right, I need it not to redirect in any case, just show the save as pop up window if everything is ok and show nothing in case of exception – Yara Mousa Aug 26 '14 at 10:21

0 Answers0