I have a form that contains some normal form input fields and then a file upload option. The file upload is optional however. When the users selects a file and clicks the upload button the form enctype
must be set to multipart/form-data
however if the user does not add a file but just clicks submit then the enctype
must be set to application/x-www-form-urlencoded
which is the default. If the enctype
is not set correctly I get errors on the submit. I have tried changing the enctype
dynamically as the upload button it pressed but that doesn't seem to work.
My page:
<h:form id="master">
... some fields and other stuff
<p:fileUpload value="#{controller.file}" mode="simple"/>
<p:commandButton
onstart="document.getElementById('master').enctype = 'multipart/form-data';"
value="Upload" ajax="false" actionListener="#{controller.uploadFile}" />
<p:commandButton value="Submit" id="submit"
actionListener="#{controller.buttonSubmit}"
validateClient="true" ajax="false"/>
</h:form>
With this setup when I try to upload a file i get an error:
UT010016: Not a multi part request
If i move the multipart/form-data
to the h:form
tag then I can upload files, but I can't submit the page. Is there a way to upload files and handle a normal form submit on the same page? Or perhaps suggestions of another way to do it.