Use an external button for rich: fileUpload

I use the rich: fileUpload component to upload files to my server, the problem is that these files correspond to the form that the user fills, so I want to use one external button for this.

The user selects the files to upload, fills out the form, and then the "Submit" button at the bottom of the page. Upload the file using the form. I tried this as follows:

I can hide the button inside the fileUpload panel so that the user does not click on it.

<rich:fileUpload id="fileUploadId" style="width: 100%; height: 130px;" fileUploadListener="#{documentsBean.listener}" maxFilesQuantity="1" uploadButtonClass="display-none" uploadButtonClassDisabled="display-none"> </rich:fileUpload> 

And what I tried with the button

 <a4j: commandButton id="uploadFormButton" value="Attach" onclick="#{rich:component('fileUploadId')}.submitForm();" oncomplete="#{rich:component('fileUploadId')}.clear(); return false;"/> 

But that will not work.

+6
source share
1 answer

I don't know if there is a way to do exactly what you want, but here is another solution that you can use:

 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:rich="http://richfaces.org/rich" xmlns:a4j="http://richfaces.org/a4j" xmlns:st="http://spectotechnologies.com/jsf" xmlns:t="http://myfaces.apache.org/tomahawk"> ... <h:form enctype="multipart/form-data"> ... your fields ... <t:inputFileUpload value="#{bean.document}" /> <h:commandButton value="Submit" actionListener="#{bean.onButtonSubmitClick}" /> </h:form> </html> 

and bean:

 @ManagedBean @RequestScoped public class Bean { private UploadedFile m_oDocument; public void setDocument(UploadedFile p_oDocument) { m_oDocument = p_oDocument; } @UploadedFileNotEmpty @UploadedFileSize(max="10000000") @UploadedFileExtension(accept="doc,docx,pdf,txt,rtf,xls,xlsx,zip,rar,jpg,jpeg,jpe,bmp,gif,png,csv,ppt,pptx,odp,pic,odt,ods") public UploadedFile getDocument() { return m_oDocument; } public void onButtonSubmitClick(ActionEvent p_oEvent) { ... } } 

Hope this helps!

+2
source

Source: https://habr.com/ru/post/898485/


All Articles