JSF Showcase

Note

The enclosing form must have enctype="multipart/form-data"

h:inputFile

HtmlInputFile is a UIInput component for JSF 2.2+ that renders an <input> element with type="file".

General Usage

By default, one file can be uploaded as a non-Ajax (full-page) postback.

Source Code

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

	<h:form enctype="multipart/form-data">
		<h:inputFile value="#{htmlInputFileBackingBean.uploadedPart}" />
		<hr />
		<h:commandButton value="#{i18n['submit']}" />
		<ui:include src="../htmlInputFileCommon.xhtml" />
	</h:form>

</ui:composition>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
	xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
	<c:set value="#{htmlInputFileModelBean.uploadedFiles.size()}" var="totalFiles" />
	<h:dataTable id="modelValue" rendered="#{totalFiles > 0}" styleClass="files"
		value="#{htmlInputFileModelBean.uploadedFiles}" var="uploadedFile">
		<h:column>
			<h:commandButton actionListener="#{htmlInputFileBackingBean.deleteUploadedFile}"
				image="#{resource['images:icon-delete.png']}"
				onclick="if (! confirm('#{i18n['are-you-sure-you-want-to-delete-this']}')) {return false;}"
				value="#{uploadedFile.id}">
				<f:ajax render="@form" />
			</h:commandButton>
		</h:column>
		<h:column>
			<f:facet name="header">
				<h:outputText rendered="#{totalFiles > 0}" value="#{i18n['file-name']}" />
			</f:facet>
			<h:outputText value="#{uploadedFile.name}" />
		</h:column>
		<h:column>
			<f:facet name="header">
				<h:outputText rendered="#{totalFiles > 0}" value="#{i18n['size']}" />
			</f:facet>
			<h:outputText value="#{uploadedFile.size}" />
		</h:column>
	</h:dataTable>
</ui:composition>
@ViewScoped
@ManagedBean
public class HtmlInputFileModelBean implements Serializable {

	// serialVersionUID
	private static final long serialVersionUID = 221172404377673109L;

	// Logger
	private static final Logger logger = LoggerFactory.getLogger(HtmlInputFileModelBean.class);

	// Private Data Members
	private List<UploadedFile> uploadedFiles;

	public List<UploadedFile> getUploadedFiles() {
		return uploadedFiles;
	}

	@PostConstruct
	public void postConstruct() {
		this.uploadedFiles = new ArrayList<UploadedFile>();
	}

	@PreDestroy
	public void preDestroy() {

		for (UploadedFile uploadedFile : uploadedFiles) {

			try {
				uploadedFile.delete();
			}
			catch (IOException e) {
				logger.error(e);
			}
		}

		uploadedFiles = null;
	}
}
@RequestScoped
@ManagedBean
public class HtmlInputFileBackingBean {

	// Logger
	private static final Logger logger = LoggerFactory.getLogger(HtmlInputFileBackingBean.class);

	// Injections
	@ManagedProperty(value = "#{htmlInputFileModelBean}")
	private HtmlInputFileModelBean htmlInputFileModelBean;

	// Private Data Members
	private Part uploadedPart;

	public void deleteUploadedFile(ActionEvent actionEvent) {

		UICommand uiCommand = (UICommand) actionEvent.getComponent();
		String fileId = (String) uiCommand.getValue();

		try {
			List<UploadedFile> uploadedFiles = htmlInputFileModelBean.getUploadedFiles();

			UploadedFile uploadedFileToDelete = null;

			for (UploadedFile uploadedFile : uploadedFiles) {

				if (uploadedFile.getId().equals(fileId)) {
					uploadedFileToDelete = uploadedFile;

					break;
				}
			}

			if (uploadedFileToDelete != null) {
				uploadedFileToDelete.delete();
				uploadedFiles.remove(uploadedFileToDelete);
				logger.debug("Deleted file=[{0}]", uploadedFileToDelete.getName());
			}
		}
		catch (Exception e) {
			logger.error(e);
		}
	}

	public Part getUploadedPart() {
		return uploadedPart;
	}

	public void setHtmlInputFileModelBean(HtmlInputFileModelBean htmlInputFileModelBean) {

		// Injected via @ManagedProperty annotation
		this.htmlInputFileModelBean = htmlInputFileModelBean;
	}

	public void setUploadedPart(Part uploadedPart) {
		this.uploadedPart = uploadedPart;

		String id = Long.toString(((long) hashCode()) + System.currentTimeMillis());
		UploadedFile uploadedFile = new UploadedFilePart(uploadedPart, id, UploadedFile.Status.FILE_SAVED);
		htmlInputFileModelBean.getUploadedFiles().add(uploadedFile);
	}
}
Liferay Faces Bridge Implementation 4.0.0 + Showcase Common 3.0.0 + Liferay Faces Util 3.0.1-SNAPSHOT + Mojarra 2.2.13