Is it possible to reset the file restriction counter to p: fileUpload?

We have it p:fileUploadso that the user can upload an avatar image. If he wants, he should be able to upload different files in short sequences without reloading the view; this will replace his current avatar every time. However, if the parameter is fileLimitset to 1, it must reload the page in order to use the component again. If it is larger (or unlimited (= 0)), it can upload multiple files at once, which makes little sense. The value of multiple-attribute, if set to false, limits only the viewing of the file to the single file selection dialog box; It can be reopened to add more files as desired. Can I upload any number of files, but not more than one file at a time? We are using Primefaces 4.0.

+4
source share
3 answers

I came up with this workaround that does not reset the file restriction counter itself, but instead resets the fileUpload component to reset the counter. Thus, the parameter fileLimitlimits the number of files per download, not per page.

First, let's define a function reset()in the PrimeFaces FileUpload component:

PrimeFaces.widget.FileUpload.prototype.reset = function() {
    this.clearMessages();
    return this.init(this.cfg);
}

This method resets the component by calling init()with the current configuration.

Then you just need to call this method in FileUploadEvent as follows:

public void uploadFile(FileUploadEvent event) {
    RequestContext.getCurrentInstance().execute("PF('fileUploadWidgetVar').reset();");
}

And now every time you process the downloaded files, the counter is reset, because the component is also reset.

+5
source

. update = "@this" p: fileUpload .

+14

Did you use the operator auto="true"? Keep in mind that the default setting in Primeface does not automatically download files unless activated by the user.

+1
source

All Articles