1
I am having trouble trying to recover that file,file
, in my bean.
.xhtml
<h:form id="form" enctype="multipart/form-data">
<p:fileUpload value="#{bean.file}"
skinSimple="true" mode="simple" />
<p:commandButton value="Enviar" ajax="true"
action="#{bean.addFile}" /></h:form>
Java bean.
@ManagedBean
@ViewScoped
public class Bean{
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setfile(UploadedFile file) {
this.file = file;
}
public void addFile() {
try {
String fileName = file.getFileName();
File fileOut = new File(fileName);
FileOutputStream fileOutputStream = new FileOutputStream(fileOut);
fileOutputStream.write(file.getContents());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
When trying to access the attribute file
, in the method addFile()
, called by commandButton
, the attribute file
is not set with the file I uploaded, is set to null. I can’t find the problem, I’ve searched several sources and was unsuccessful.
The Imports:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.model.UploadedFile;
Try changing the attribute
ajax
ofcommandButton
forfalse
.– Homer Simpson
@Henry already tried, setando
ajax="false ",
the methodaddFile()
nor called is– Henrique Santiago
Change the attribute
action
ofcommandButton
foractionListener
.– Homer Simpson
@Henry did not understand. attribute
action
ofcommandButton
forcommandButton
?– Henrique Santiago