我有一個數據表,它允許你下載文件。有用。但是,當我嘗試下載文件後,返回列表中的第一個。如何在Primefaces的過濾數據表中下載文件?
此圖顯示在teste4.txt上單擊時下載了相同的文件。這是對的。
這其他的圖片顯示,在文件teste4.txt點擊後,過濾後,下載的文件被teste1.txt
這是一個包含數據表我的文件:
<h:form id="hFormListaArquivosRegiao2" enctype="multipart/form-data">
<p:dataTable id="pDataTableListaArquivos" var="arquivo" value="#{arquivoBean.listaArquivos}" filteredValue="#{arquivoBean.filteredListaArquivos}">
<p:column id="pColumnNomeArquivo" headerText="#{msg.NomeDoArquivo}" sortBy="#{arquivo.nomeArquivo}" filterMatchMode="contains" filterBy="#{arquivo.nomeArquivo}">
<h:commandLink action="#{arquivoBean.download}" title="#{arquivo.nomeArquivo}">
<h:outputText value="#{arquivo.nomeArquivo}" />
<f:setPropertyActionListener target="#{arquivoBean.arquivo}" value="#{arquivo}" />
</h:commandLink>
</p:column>
</h:form>
這是方法backingBean負責下載:
public void download() throws Exception {
logger.debug("NOME ARQUIVO: "+ ContextoBean.CAMINHO_ARQUIVOS + arquivo.getNomeArquivo() +", "+ arquivo.getNomeArquivo());
FacesContext facesContext = FacesContext.getCurrentInstance();
pushArquivo(facesContext.getExternalContext(), ContextoBean.CAMINHO_ARQUIVOS + arquivo.getNomeArquivo(), arquivo.getNomeArquivo());
facesContext.responseComplete();
}
這是backingBean的助手方法,負責下載:
private void pushArquivo(ExternalContext externalContext, String nomeArquivo, String nomeDeExibicao) throws IOException {
// Info sobre a lógica usada: http://stackoverflow.com/questions/1686821/execute-backing-bean-action-on-load
File descritoArquivo = new File(nomeArquivo);
int length = 0;
OutputStream os = externalContext.getResponseOutputStream();
String extencaoArquivo = externalContext.getMimeType(nomeArquivo);
externalContext.setResponseContentType((extencaoArquivo != null) ? extencaoArquivo : "application/octet-stream");
externalContext.setResponseContentLength((int) descritoArquivo.length());
externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + nomeDeExibicao + "\"");
// Stream to the requester.
byte[] bbuf = new byte[1024];
DataInputStream in = new DataInputStream(new FileInputStream(descritoArquivo));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
os.write(bbuf, 0, length);
}
in.close();
}
您是否嘗試過使用Primefaces' [下載組件](http://www.primefaces.org/showcase/ui/fileDownload.jsf)?它應該與自己的過濾器兼容 –
是的,我試過了。我實現了這樣的事情:[Primefaces p:fileDownload with datatable](http://stackoverflow.com/questions/14144306/primefaces-pfiledownload-with-datatable) – fenix