ICEfaces libary in classpath prevents Save As dialog from popping up on file download

随声附和 提交于 2019-11-26 14:53:10

问题


As soon as I add the librarys icefaces.jar icepush.jar icefaces_ace.jar to my classpath in order to use ACE components, my SaveAs dialog won't popup? I'm not sure if this is a bug but without the librarys in classpath it works. Here's my save as method :

    public void downloadFile(String propertyPath) throws IOException {

     ProxyFile fileToDownload = repBean.downloadFile(propertyPath);

     FacesContext facesContext = FacesContext.getCurrentInstance();
     ExternalContext externalContext = facesContext.getExternalContext();
     HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

     response.reset();         response.setContentType(fileToDownload.getContentType()); 
     response.setHeader("Content-Length", String.valueOf(fileToDownload.getLength()));
     response.setHeader("Content-disposition", "attachment; filename=\"" + fileToDownload.getName() + "\""); 

     BufferedInputStream input = null;
     BufferedOutputStream output = null;


     try {
         input = new BufferedInputStream(fileToDownload.getContent());
         output = new BufferedOutputStream(response.getOutputStream());

         byte[] buffer = new byte[10240];
         for (int length; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
         }
     } finally {
         output.close();
         input.close();
         facesContext.responseComplete(); 
        }
     }

回答1:


You can't download files using ajax.

Ajax is under the covers executed by JavaScript's XMLHttpRequest object. The request will be successfully executed and the response will be successfully retrieved. However, JavaScript has no facility to write the response to client's disk file system, nor to force a Save As dialogue with the given response. That would be a huge security breach.

The cause of your concrete problem is ICEfaces itself. Namely, when you integrate ICEfaces in a JSF web application, all standard <h:commandXxx> links/buttons will silently be turned into ajax-enabled ones which indeed causes confusion among starters. Make sure that the download link/button isn't implicitly using ICEfaces-introduced ajax facility. As per their wiki page on the subject, you need to explicitly nest a <f:ajax disabled="true"> to disable this.

Disable Ajax for a Component

You can also disable Ajax at the level of the individual component:

<h:commandButton value="Send" actionListener="#{bean.sendMessage}">
    <f:ajax disabled="true"/>
</h:commandButton>

Apply it on your download link/button.



来源:https://stackoverflow.com/questions/13420958/icefaces-libary-in-classpath-prevents-save-as-dialog-from-popping-up-on-file-dow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!