0

fileUploadDownload.xhtml

    <h:form id="form" enctype="multipart/form-data">
            <p:growl id="growl" sticky="true" showsDetail="true" />
            <div id="header">
                <ui:insert name="header">
                    <ui:include src="/templates/header.xhtml" />
                </ui:insert>
            </div>

            <p:dialog modal="true" widgetVar="statusDialog" header="Status"
                draggable="false" closable="false" resizable="false">
                <p:graphicImage value="/resources/images/ajaxloading.gif" />
            </p:dialog>
            <p:growl id="msgs" showDetail="true" />

            <p:tabView>
                <p:tab title="Upload File">

                </p:tab>

                <p:tab title="Download File">

                    <p:commandButton value="Download" icon="ui-icon-circle-arrow-s"
                        ajax="false"
                        actionListener="#{fileDownloadView.download(fileDownloadView.selectedFile)}">
                        <f:setPropertyActionListener value="#{dfTable}"
                            target="#{fileDownloadView.selectedFile}" />
                    </p:commandButton>

                    <p:dataTable id="downFilesTable" var="dfTable" paginator="true"
                        rows="15" rowsPerPageTemplate="5,10,15,20,25,30"
                        sortBy="#{dfTable.bankName}" expandableRowGroups="true"
                        value="#{copyFilesView.copyFilesModelList}" widgetVar="table"
                        selection="#{fileDownloadView.selectedFile}"
                        rowKey="#{dfTable.fileName}">
                        <f:facet name="header">
                            Copy Output Files
                        </f:facet>

                        <p:column selectionMode="single"
                            style="width:16px;text-align:center" />
                        <p:headerRow>
                            <p:column colspan="5" headerText="Bank Name">
                                <h:outputText value="#{dfTable.bankName}" />
                            </p:column>
                        </p:headerRow>

                        <p:column headerText="Bank Identifier"
                            filterBy="#{dfTable.bankIdentifier}" footerText="contains"
                            filterMatchMode="contains">
                            <h:outputText value="#{dfTable.bankIdentifier}" />
                        </p:column>

                        <p:column headerText="Output File Name"
                            filterBy="#{dfTable.fileName}" footerText="contains"
                            filterMatchMode="contains">
                            <h:outputText value="#{dfTable.fileName}" />
                        </p:column>

                        <p:column headerText="File Type" filterBy="#{dfTable.fileType}"
                            footerText="equals" filterMatchMode="equals">
                            <f:facet name="filter">
                                <p:selectOneButton onchange="PF('table').filter()">
                                    <f:selectItem itemLabel="All" itemValue="" />
                                    <f:selectItem itemLabel="Update" itemValue="Update" />
                                    <f:selectItem itemLabel="Full" itemValue="Full" />
                                </p:selectOneButton>
                            </f:facet>
                            <h:outputText value="#{dfTable.fileType}" />
                        </p:column>

                        <p:column headerText="File Timestamp" sortBy="#{dfTable.timestamp}">
                            <h:outputText value="#{dfTable.timestamp}" />
                        </p:column>

                    </p:dataTable>
                </p:tab>
            </p:tabView>

        </h:form>

FileDownloadView.java

public class FileDownloadView
{
    private CopyFilesModel selectedFile;

    public CopyFilesModel getSelectedFile()
    {
        return selectedFile;
    }

    public void setSelectedFile(CopyFilesModel selectedFile) 
    {
        this.selectedFile = selectedFile;
    }

    public void download(CopyFilesModel selectedFile) throws IOException
    {
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        String filename = selectedFile.getFileName();
    // not sure about the rest of the code... I've tried many different ways...
    }
}

Model

public class CopyFilesModel implements Serializable
{
    private String bankName;
    private String bankIdentifier;
    private String fileName;
    private String fileType;
    private String timestamp;

    // *******************GETTERS & SETTERS *****************************
    public String getBankName() 
    {
        return bankName;
    }
    public void setBankName(String bankName) 
    {
        this.bankName = bankName;
    }
    public String getBankIdentifier() 
    {
        return bankIdentifier;
    }
    public void setBankIdentifier(String bankIdentifier) 
    {
        this.bankIdentifier = bankIdentifier;
    }
    public String getFileName() 
    {
        return fileName;
    }
    public void setFileName(String fileName) 
    {
        this.fileName = fileName;
    }
    public String getFileType() 
    {
        return fileType;
    }
    public void setFileType(String fileType) 
    {
        this.fileType = fileType;
    }
    public String getTimestamp() 
    {
        return timestamp;
    }
    public void setTimestamp(String timestamp)
    {
        this.timestamp = timestamp;
    }  
}

Information

I have a datatable that is being populated from another xhtml page's backing bean as they use the same information for the datatable. In the code above I want to select a row/file from the datatable and then use the commandButton to download the file. I want the user to choose where to save the file to. Can someone help me to download the file from the directory /moe/del/hc/arc/20180905/send by passing an object from the datatable into my download method in the backing bean and then save the file to wherever the user specified? The file is of type File and looks something like this, HTUZ001D, with no extension..

Moe
  • 39
  • 4
  • Possible duplicate of [How to provide a file download from a JSF backing bean?](https://stackoverflow.com/questions/9391838/how-to-provide-a-file-download-from-a-jsf-backing-bean) – Jasper de Vries Sep 05 '18 at 11:12
  • @JasperdeVries I have looked at that before, and it still didn't work. – Moe Sep 05 '18 at 12:22

0 Answers0