0

I am trying to download the template file (Template.xls) from the server. But the Open / Save as dialogue box is not popping up to download the file. I am using Jboss eap 6.3

Here is my code,

OutputStream os = null;
InputStream is = null;
try {
    URL resource = FacesContext.getCurrentInstance().getExternalContext().getResource("/WEB-INF/Template.xls");
    is = resource.openStream();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpServletResponse resp = (HttpServletResponse) facesContext.getExternalContext().getResponse();
    resp.setHeader("Content-disposition", "attachment; filename=Template.xls"); 
    resp.setContentType("application/vnd.ms-excel");

    os = resp.getOutputStream();                        
    IOUtils.copy(is, os);
    os.flush();
    facesContext.responseComplete();
} catch (Exception e) {
    StatusMessages.instance().add(StatusMessage.Severity.ERROR, "Error occured while downloading IPS document");

} finally {
    IOUtils.closeQuietly(os);
    IOUtils.closeQuietly(is);
}

Calling this file template download in richfaces menu like his,

<rich:menuGroup style="font-size:10px;" label="Utilities" icon="img/TolMaintenance.png">
                        <rich:menuItem style="font-size:10px;" action="#{excelTemplate.downloadExcelTemplate}" label="Template" icon="img/catadmin.png" >
                        </rich:menuItem>
                    </rich:menuGroup>

There is no error message on the server log also the file is present in the server location and while debug I have seen the URL is pointing to the same location. I have checked the following link,

Open/Save as... dialog box not showing

The same issue I am facing. I have tried the suggestions provided over there but nothing is working out.

Can anyone please help me to fix the issue?

Community
  • 1
  • 1
Sudha
  • 220
  • 2
  • 16
  • @Balus - As per the existing answer of the http://www.iana.org/assignments/media-types question, I have modified the code with JSF2 section. My content type is correct as per the url(http://www.iana.org/assignments/media-types). There is no option to turn off the ajax in my code. please check my code I have added the JSF code. – Sudha Jun 08 '16 at 20:28
  • 1
    The content type only instructs the webbrowser which client application to associate with the provided file. If the *Save As* dialog already doesn't show up in first place, then this part is not relevant for now. As to ajax, you **cannot** download files with ajax. Try yourself with a regular `` to just test whether the bean action method functions properly. If it works fine, then the problem is evidently not there. – BalusC Jun 08 '16 at 20:30
  • BalusC - Thank you for the quick responses. I am using rich:menuItem and calling the action. The default mode attribute of rich:menuItem is server, I haven't set the mode to ajax. I am not using ajax to download the file. I will try with h:commandButton now. Thanks again for the suggestion. – Sudha Jun 08 '16 at 20:35
  • Balus - The file download code is working fine with h:commandButton. I will check why it is not working with rich:menuItem. Thank you for your suggestions. – Sudha Jun 08 '16 at 20:45

0 Answers0