I would like to know how to use dropzone.js with JSF for upload a file. In the documentation (http://www.dropzonejs.com/#usage) it says that using dropzone is like using:
<input type="file" name="file" />
But I don't know how to implement the server-side part in JSF to get the file and store it in disk.
It appears that you don't exactly realize that JSF is in the context of this question merely a HTML code generator. JSF offers a
<h:inputFile>
component which generates a HTML<input type="file">
element.Try it yourself:
If you open the JSF page in a webbrowser and do rightclick and View Page Source, then you should see something like this:
Look, there is your
<input type="file">
element!Now, if we configure Dropzone as per its documentation (and you install the
dropzone.js
file in your JSF project as per the instructions in How to reference CSS / JS / image resource in Facelets template?), then we should end up in the JSF page with something like this:The bean looks just like this:
There are 3 things to take into account with JSF:
paramName
option must be set to exactly thename
of the generated<input type="file">
element, which isuploadForm:file
in the above example.class="fallback"
in order to be hidden (and provide a fallback for JavaScript/Ajax-deficient clients). Do not remove them, otherwise JSF will refuse to process the uploaded file, because it needs to perform its job based on the component tree.save()
method is invoked directly by the setter. This is kind of fishy, but as Dropzone doesn't offer an opportunity to directly trigger a backing bean action method, this is the easiest workaround. Another workaround is to attach avalueChangeListener
on the<h:inputFile>
and queue the event to theINVOKE_APPLICATION
phase as per How to get updated model values in a valueChangeListener method?Your next question shall probably be "How should I save it?". In that case, continue here: