简体   繁体   English

在 p:dataTable 中的 p:ajax rowSelect 事件期间下载文件

[英]Downloading file during p:ajax rowSelect event in p:dataTable

I want to download a document file by row click on primefaces datatable.我想通过单击 primefaces 数据表逐行下载文档文件。 I tried to call the action from Bean by using ajax like this:我尝试使用这样的 ajax 从 Bean 调用操作:

<p:dataTable
            id="docId"
            value="#{testBean.document}"
            var="doc"
            selectionMode="single"
            selection="#{testBean.selectedDoc}"
            >

            <p:ajax event="rowSelect" listener="#{testBean.actionDownload}"/>

            <p:column>
                ...
            </p:column>
                ...
</p:dataTable>

But the following code does not execute what I want.但是下面的代码没有执行我想要的。 The logic of my action is correct.我的行动逻辑是正确的。 But seems so that the downloading document does not work with ajax Request.但似乎下载文件不适用于ajax请求。 There is no reaction and the download does not executed.没有反应,下载不执行。

public String actionDownload() {
    try {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

        StringBuffer contentDisposition = new StringBuffer();
        contentDisposition.append("attachment;");
        contentDisposition.append("filename=\"");
        contentDisposition.append(name);
        contentDisposition.append("\"");

        response.reset();
        response.setContentType("application/x-download");

        response.setHeader("Content-Disposition", contentDisposition.toString());
        ServletOutputStream out = response.getOutputStream();
        response.setContentType("application/x-download");
        out.write(output);
        out.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    FacesContext.getCurrentInstance().responseComplete();
    return null;
}

Is there an alternative to download a file by click on Datatable row?是否有另一种方法可以通过单击数据表行来下载文件?

You cannot download files with JSF/PrimeFaces Ajax.您无法使用 JSF/PrimeFaces Ajax 下载文件。 Make it a non-Ajax request instead.改为将其设为非 Ajax 请求。

Replace ajax listener in <p:ajax> by a GET request in oncomplete something like:<p:ajax>的 ajax 侦听器替换为oncomplete的 GET 请求,例如:

<p:ajax event="rowSelect" oncomplete="window.location='#{request.contextPath}/download/#{doc.id}'"/>

And replace backing bean method actionDownload() by a plain vanilla Servlet something like:并将支持 bean 方法actionDownload()替换为一个普通的 vanilla Servlet,例如:

@WebServlet("/download/*")
public class Download extends HttpServlet {

    @Inject
    private DocumentService service;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Document document = service.find(Long.valueOf(request.getPathInfo().substring(1)));
        response.setContentType(getServletContext().getMimeType(document.getFileName()));
        response.setContentLength(document.getContent().length);
        response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(document.getFileName(), "UTF-8") + "\"");
        response.getOutputStream().write(document.getContent());
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM