Problem with p:fileDownload in PF10

UI Components for JSF
Post Reply
jhula
Posts: 2
Joined: 07 Sep 2017, 10:00

18 Jan 2022, 15:23

I have problem with the p:fileDownload. I followed the example https://primefaces.github.io/primefaces ... miccontent I pass parameters to the fileDownloadBean. The resourceBean is ViewScoped.

Code: Select all

<p:commandButton value="download" process="@this">
	<f:param name="fileName" value="#{resourceBean.fileName}" />
	<f:param name="filePath" value="#{resourceBean.filePath}" />
	<p:fileDownload value="#{fileDownloadBean.file}" />
</p:commandButton>
The FileDownloadBean is called twice. On the first call the values for fileName and filePath are there, but on the second call they are null.

Code: Select all

@Named
@RequestScoped
public class FileDownloadBean {

	private StreamedContent file;

	public FileDownloadBean() throws ServiceException {
		String fileName = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("fName");
		String filePath = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("filePath");
		try {
			FileInputStream fis= new FileInputStream(filePath);
			file = DefaultStreamedContent.builder().name("fileName ").contentType(URLConnection.guessContentTypeFromName("fileName .html"))
				.stream(() -> fis).build();
		} catch (FileNotFoundException fnf) {
			throw fnf;
		}

	}

	public StreamedContent getFile() {
		return file;
	}

}
PrimeFaces 10.0.0
Tomcat 9.0.53
JSF 2.3

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Google [Bot] and 37 guests