首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ItemNode点击后下载文件

在ItemNode点击后下载文件
EN

Stack Overflow用户
提问于 2017-10-26 21:48:55
回答 1查看 352关注 0票数 0

我通过xml菜单模型使用oracle-adf。现在我想在一个itemNode上开始下载文件,而不重定向。我尝试用方法来定义动作属性,这个方法通过javascript调用隐藏按钮的方法(这个按钮有内部的fileDownloadActionListener)。但它不起作用。这是正确的吗?或者有其他方法来决定这个问题?或者这是不可能的?

隐藏按钮代码:

代码语言:javascript
复制
<af:commandButton text="NONE"
                    id="downloadInstructionsBtn"
                    action=" "
                    visible="false"
                    clientComponent="true"
                    partialSubmit="false">
       <af:fileDownloadActionListener filename="Инструкции пользователя"
       contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                                     method="#{pageFlowScope.vocsReportsRegionController.instructionsDownload}"/> 
</af:commandButton>

物料节点编码:

代码语言:javascript
复制
<itemNode id="userInstructionsDownloadFlow" label="Инструкции пользователя"
              focusViewId="#"
              action="#{pageFlowScope.vocsReportsRegionController.invokeInstructionDownload}"
              partialSubmit="true"
              clientComponent="true"/>

Javascript cut:

代码语言:javascript
复制
function handleInstructionsDownload(event) {
    event.preventUserInput();
    var source = event.getSource().getParent();
    var downloadBtn = source.findComponent("downloadInstructionsBtn");
    var actionEvent = new AdfActionEvent(downloadBtn);
    actionEvent.preventUserInput();
    actionEvent.queue();
}

方法描述:

代码语言:javascript
复制
public void invokeInstructionDownload(){
    FacesContext context = FacesContext.getCurrentInstance();
    ExtendedRenderKitService erks =
            Service.getService(context.getRenderKit(),
                    ExtendedRenderKitService.class);

    erks.addScript(context, "handleInstructionsDownload();");
}

public void instructionsDownload(FacesContext context,
                                 OutputStream out) throws IOException{
    File f = new File("C:\\Users\\apozdnyakov\\Downloads\\Типы_контроля_время.xlsx");
    FileInputStream fis;
    byte[] b;
    try {
        fis = new FileInputStream(f);

        int n;
        while ((n = fis.available()) > 0) {
            b = new byte[n];
            int result = fis.read(b);
            out.write(b, 0, b.length);
            if (result == -1)
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    out.flush();
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-03 20:17:25

我认为你的问题有几个可能的原因。

1- handleInstructionsDownload javascript函数找不到组件,因为jsf代码的层次结构。你可以在javascript函数中添加log,以便理解这一点。

代码语言:javascript
复制
function handleInstructionsDownload(event) {
  event.preventUserInput();
  var source = event.getSource().getParent();
  var downloadBtn = source.findComponent("downloadInstructionsBtn");
  if (downloadBtn == null) {
    console.log('The component is null!');
  }
  var actionEvent = new AdfActionEvent(downloadBtn);
  actionEvent.preventUserInput();
  actionEvent.queue();
}

如果您在javascript控制台中看到此日志,则应该控制jsf代码的层次结构。并且你应该通过真实的组件id来访问按钮。

2- invokeInstructionDownload java方法无法找到或调用javascript handleInstructionsDownload函数。您可以在javascript函数的第一行添加log,如下所示:

代码语言:javascript
复制
function handleInstructionsDownload(event) {
  console.log('The js function fired!');
  event.preventUserInput();
  var source = event.getSource().getParent();
  var downloadBtn = source.findComponent("downloadInstructionsBtn");
  var actionEvent = new AdfActionEvent(downloadBtn);
  actionEvent.preventUserInput();
  actionEvent.queue();
}

如果控制台中有此日志,您应该更改您的javascript方法调用。但我认为您的Java方法是正确的:)

3-如果这些不是你的问题的解决方案,你可以像这样改变你对隐藏按钮的调用。

隐藏按钮代码:

代码语言:javascript
复制
<af:commandButton text="NONE" 
                  id="downloadInstructionsBtn" action=" "
                  visible="false"
                  clientComponent="true"
                  partialSubmit="false"
                  binding="#{pageFlowScope.vocsReportsRegionController.downloadInstructionsBtn}">
   <af:fileDownloadActionListener filename="Инструкции пользователя"
                  contentType="application/vnd.openxmlformats officedocument.spreadsheetml.sheet"
                  method="#{pageFlowScope.vocsReportsRegionController.instructionsDownload}"/> 
</af:commandButton>

ManagedBean代码:

代码语言:javascript
复制
private RichButton downloadInstructionsBtn;

public RichButton getDownloadInstructionsBtn() {
  return downloadInstructionsBtn;
}

public void setDownloadInstructionsBtn(RichButton downloadInstructionsBtn) {
  this.downloadInstructionsBtn = downloadInstructionsBtn;
}

public void invokeInstructionDownload(){
  ActionEvent actionEvent = new ActionEvent((UIComponent) downloadInstructionsBtn);
  actionEvent.queue();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46956141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档