Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

Try putting the p:fileUpload in a different h:form with enctype="multipart/form-data". That is the only way I achieve that. – Kaz Miller Aug 25, 2014 at 11:54 What do you mean by "I can't submit"? What happens when you try? It stands to reason that you can try to pull form data off the multipart request using the new servlet API's Part interface methods – kolossus Aug 25, 2014 at 20:19

Probably You are using WlidFly 8. Other application server (JBoss AS 6.1) work's fine for me with similar example.

Look at this https://code.google.com/p/primefaces/issues/detail?id=6925

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.