How to limit RichFaces 4 rich: fileUpload to upload a single file?

I am using <rich:fileUpload /> from RichFaces4, as you can see here . But, as you can see, the user can select a large number of images, but I want him to be able to select only one image.

And how do I call a method in my managed bean (which sends the image to my Amazon S3 bucket) after the download is complete?

EDIT:

 <ui:composition> <h:outputStylesheet> .top { vertical-align: top; } .info { height: 202px; overflow: auto; } .rf-fu-lst { height: 60px; } .rf-fu-itm { border: 0; } </h:outputStylesheet> <h:outputScript target="head"> jQuery.extend(RichFaces.ui.FileUpload.prototype, { __updateButtons: function() { if (!this.loadableItem && this.list.children(".rf-fu-itm").size()) { if (this.items.length) { this.uploadButton.css("display", "inline-block"); this.addButton.hide(); } else { this.uploadButton.hide(); this.addButton.css("display", "inline-block"); } } else { this.uploadButton.hide(); this.addButton.css("display", "inline-block"); } } }); </h:outputScript> <h:form id="form_user_upload_picture" > <h:panelGrid columns="2" columnClasses="top, top"> <rich:fileUpload id="upload" fileUploadListener="#{user_file_upload.listener}" acceptedTypes="jpg, gif, png, bmp" addLabel="Adicionar" clearAllLabel="Limpar todas" clearLabel="limpar" deleteLabel="apagar" doneLabel="upload realizado" sizeExceededLabel="arquivo muito grande, tente um arquivo de tamanho menor" serverErrorLabel="ocorreu um erro em nosso servidor, tente novamente por favor" uploadLabel="Enviar"> <a4j:ajax event="uploadcomplete" execute="@none" render="picture" /> </rich:fileUpload> </h:panelGrid> </h:form> </ui:composition> 
+4
source share
1 answer

This was supported in RF 3.3. But to save time for migrating RF 4.0, among others, <rich:fileUpload> received far less attention than it deserves. Currently, there are many open tickets to improve it.

Until they improve it, your best bet is to bring some custom jQuery / JS and CSS to achieve functional requirements for selecting and loading only one file.

This script does not allow the end user to upload multiple files, hiding the add button when the file is already selected:

 jQuery.extend(RichFaces.ui.FileUpload.prototype, { __updateButtons: function() { if (!this.loadableItem && this.list.children(".rf-fu-itm").size()) { if (this.items.length) { this.uploadButton.css("display", "inline-block"); this.addButton.hide(); } else { this.uploadButton.hide(); this.addButton.css("display", "inline-block"); } } else { this.uploadButton.hide(); this.addButton.css("display", "inline-block"); } } }); 

Make sure the above JS is loaded after the default RF scripts (which jQuery already includes). You can do this with <h:outputScript> in the body, which you can optionally set with target="head" .

This CSS limits the height of the file list and removes the bottom border of an individual element:

 .rf-fu-lst { height: 60px; } .rf-fu-itm { border: 0; } 

Make sure the above CSS is loaded after the default RF styles. You can do this with <h:outputStylesheet> in the body (so don't put it in the head).


And how do I call a method in my managed bean (which sends the image to my Amazon S3 bucket) after the download is complete?

Just do the job in the listener method, which is bound to the fileUploadListener attribute.

 <rich:fileUpload fileUploadListener="#{bean.upload}"> 
+4
source

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


All Articles