0

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.

Sean
  • 1,416
  • 19
  • 51
  • 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 '14 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 '14 at 20:19
  • I get the same error as mentioned `UT010016: Not a multi part request` – Sean Aug 26 '14 at 05:00

1 Answers1

0

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

  • This solution was best for me: http://stackoverflow.com/questions/19262356/file-upload-doesnt-work-with-ajax-in-primefaces-4-0-jsf-2-2-x-javax-servlet-s/19752138#19752138 – Witold Budzynski Mar 25 '15 at 14:46