In my project I have a page where there is a button named "Create Package" that opens a dialog. Within the dialog the user can upload one or more files, and, if he does a mistake, he can delete the previously associated file(s).
I have some issues with the p:upload component, that I will list below.
1) If I specify the h:form without the enctype="multipart/form-data" parameter, the file association works fine, butI obtain the following error when trying to delete the file:
WARNING: javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is application/x-www-form-urlencoded
javax.faces.FacesException: javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is application/x-www-form-urlencoded
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:44)
at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:44)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
...
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/sism_webapp] threw exception [org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is application/x-www-form-urlencoded] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is application/x-www-form-urlencoded
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:806)
at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:261)
at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:285)
at org.apache.catalina.connector.Request.parseParts(Request.java:2722)
2) The same behaviour occurs adding the enctype="multipart/form-data" property only.
3) If I add the following property to the delete button:
onclick="$('#frm_creaDocumenti\\:fu_upload').('disabled','disabled');"
the delete action performs correctly, but the dialog closes automatically.
Is there a solution to perform the document deletion AND maintain the dialog opened?
Environment: Primefaces 4.0, Tomcat 7.0.40
XHTML page
<p:growl id="growl" showDetails="true" />
<p:commandButton id="btn_crea" value="CREA"
onclick="PF('creaPackage').show()" type="button" />
<p:dialog id="dlg_crea" header="Crea Package"
widgetVar="creaPackage" width="95%" position="top"
modal="true" hideEffect="fade" dynamic="true">
<!-- enctype="multipart/form-data" -->
<h:form id="frm_creaDocumenti">
<p:fileUpload id="fu_upload" value="#{packageBean.fileUpload}"
widgetVar="wv_upload"
uploadLabel="ASSOCIA" label="SCEGLI FILE" cancelLabel="ANNULLA SCELTA"
update="@form :growl"
fileUploadListener="#{packageBean.associaDocumentoCrea}"
process="@form" mode="advanced" sizeLimit="1000000">
</p:fileUpload>
<!-- onclick="$('#frm_creaDocumenti\\:fu_upload').('disabled','disabled');" -->
<p:commandButton id="deleteDocumentButton"
ajax="true" value="Elimina"
actionListener="#{packageBean.eliminaDocumenti('CREA')}"
update=":growl" />
<p:dataTable id="documentTable" var="doc" value="#{packageBean.documenti}" rowKey="#{doc.id}"
scrollable="true" scrollHeight="270"
selection="#{packageBean.documentiSelezionati}" selectionMode="multiple">
<p:ajax event="rowSelect" listener="#{packageBean.onRowSelect}" update="@this" />
<p:column headerText="Codice">#{doc.codice}</p:column>
<p:column headerText="Titolo">#{doc.titolo}</p:column>
<p:column headerText="Autore">#{doc.autore}</p:column>
</p:dataTable>
</h:form>
</p:dialog>
Managed Bean
public void associaDocumentoCrea(FileUploadEvent event)
{
// call "write file" procedure...
}
public void eliminaDocumenti(String azione)
{
// call "delete file" procedure...
}
web.xml
<!--
According to the Primefaces User Guide, I do not need to set filter
and filter-mapping for the file upload if I use "auto"
-->
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto</param-value>
</context-param>
pom.xml
<!--
I added the following Maven dependencies to let the file upload
component work
-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>