I have a p:uploadFile component in order to upload a file. In addition i have an upload button that does upload the file and is sets in some variables the filename. The file is uploaded correctly and set also correctly in the corresponding variable.
The problem is when i finally submit the entire form to save all my changes. At this point the variable holding the uploaded file, is not null anymore, but it seems that the there is no file anymore in my variable since the filename is empty as also its content.
In detail.
My xhtml page:
<h:form name="myUploadForm" enctype="multipart/form-data">
<p:commandButton action="#{controller.saveAllChanges}" value="Save" ajax="false" />
<p:dataTable....>
<p:column ... var="currentTableRow">
<p:commandButton value="Upload" ajax="false" title="#{msg.upload}"
action="#{controller.uploadFile(currentTableRow)}"
update=":table" />
<p:fileUpload id="upload" value="#{currentTableRow.myUploadedFile}" mode="simple"
skinSimple="true" label="Browse" auto="true" />
</column>
</p:dataTable>
</h:form>
My controller:
@ManagedBean
@ViewScoped
public class Controller implements Serializable {
...
public void uploadFile(TempDTO selectedDTO) {
FacesContext context = FacesContext.getCurrentInstance();
if (selectedDTO.getMyUploadedFile() != null && selectedDTO.getMyUploadedFile().getSize() > 0) {
...
}
}
public saveAllChanges(){
....
}
}
My TempDTO contains an UploadedFile as seen below. The datatable seen in my form displays a list of TempDTOs:
public class TempDTO {
...
...
private UploadedFile myUploadedFile;
public UploadedFile getMyUploadedFile() {
return myUploadedFile;
}
public void setMyUploadedFile(UploadedFile myUploadedFile) {
this.myUploadedFile = myUploadedFile;
}
}
The problem is noticed when:
- I select a file and press the upload button in a specific row of the table.
- Debugging the uploadFile method in my Controller when pressing the upload button shows that everything is correct and that the file is correctly set. At that stage i can see its filesize, filename etc.
- Now without performing any other action in between i press the "Save" button to save all changes in my form.
- At this stage all changes are submitted correct except of the file. The myUploadedFile inside the TempDTO has no filename as also its size is zero.
My thought at this stage was that when i submit the entire form in order to save all changes, the value="#{currentTableRow.myUploadedFile}" i have in the upload component replaces the file for each table entry.
UPDATE
I made following changes but i still had no luck .
I added an UploadedFile inside my controller in order to manually set the uploaded file inside my TempDTO:
@ManagedBean
@ViewScoped
public class Controller implements Serializable {
private UploadedFile myUploadedFile;
public UploadedFile getMyUploadedFile() {
return myUploadedFile;
}
public void setMyTemplateFile(UploadedFile myUploadedFile) {
this.myUploadedFile = myUploadedFile;
}
public void uploadFile(TempDTO selectedDTO) {
FacesContext context = FacesContext.getCurrentInstance();
selectedDTO.setTemplateFile(myUploadedFile);
if (selectedDTO.getMyUploadedFile() != null && selectedDTO.getMyUploadedFile().getSize() > 0) {
...
}
}
public saveAllChanges(){
....
}
}
Also update my uploadFile component to point to the UploadedFile inside my controller and not in my DTO:
<p:fileUpload id="upload" value="#{controller.myUploadedFile}" mode="simple"
skinSimple="true" label="Browse" auto="true" />
No when i press on the "Save" button and submit the entire form again and call saveAllChanges() , myUploadedFile has indeed a filename , but methods like getContents() or getSize() return a java.nio.file.NoSuchFileException so i still do not have the content of the file.