类路径中的 ICEfaces 库可防止在文件下载时弹出另存为对话框 [英] ICEfaces libary in classpath prevents Save As dialog from popping up on file download

查看:14
本文介绍了类路径中的 ICEfaces 库可防止在文件下载时弹出另存为对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦我将库 icefaces.jar icepush.jar icefaces_ace.jar 添加到我的类路径以使用 ACE 组件,我的 SaveAs 对话框就不会弹出?我不确定这是否是一个错误,但如果没有类路径中的库,它就可以工作.这是我的另存为方法:

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(); 
        }
     }

推荐答案

您无法使用 ajax 下载文件.

You can't download files using ajax.

Ajax 由 JavaScript 的 XMLHttpRequest 对象执行.将成功执行请求并成功检索响应.但是,JavaScript 无法将响应写入客户端的磁盘文件系统,也无法强制与给定响应进行另存为对话.这将是一个巨大的安全漏洞.

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.

您的具体问题的原因是 ICEfaces 本身.也就是说,当您将 ICEfaces 集成到 JSF Web 应用程序中时,所有标准的 链接/按钮将被默默地转换为启用 ajax 的链接/按钮,这确实会导致初学者之间的混淆.确保下载链接/按钮没有隐式地使用 ICEfaces 引入的 ajax 工具.根据他们关于该主题的维基页面,您需要明确嵌套一个 来禁用它.

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.

您还可以在单​​个组件级别禁用 Ajax:

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.

这篇关于类路径中的 ICEfaces 库可防止在文件下载时弹出另存为对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆