File Upload and processValueChange ....
Hello,
In the JSC2 File Upload's tutorial a click on the "upload file" button, uploads the file and displays some informations about it in the same time.
What I want to do is slightly different:
I want to first display some information about uploaded file once the user click on browse button, and then upload the file when the user click on the "upload file " button.
To do that I put the code that updating all the necesary textfield (file name, file size, etc ...) in the processValueChange method.
You have also to store the UploadedFile in the session bean and init since after processValueChange method page is refreshing (so you loose the value ...)
Finaly the code look like that.
publicvoid init()
{
super.init();
// (...)
UploadedFile uploadedFile = this.getSessionBean1().getUploadedFile();
if(uploadedFile !=null)
{
//System.out.println("uploadedFile= "+uploadedFile);
//System.out.println("uploadedFile.getOriginalName()= "+uploadedFile.getOriginalName());
fileUpload.setUploadedFile(uploadedFile);
}
}
publicvoid fileUpload_processValueChange(ValueChangeEvent event)
{
UploadedFile uploadedFile = (UploadedFile)event.getNewValue();
this.getSessionBean1().setUploadedFile(uploadedFile);
// updating some components ....
}
public String uploadButton_action()
{
UploadedFile uploadedFile = this.getSessionBean1().getUploadedFile();
String uploadedFileName = uploadedFile.getOriginalName();
String justFileName = uploadedFileName.substring(uploadedFileName.lastIndexOf(File.separatorChar) + 1 );
try
{
ServletContext theApplicationsServletContext = (ServletContext) this.getExternalContext().getContext();
String realPath = theApplicationsServletContext.getRealPath("/resources/img");
File file =new File(realPath + File.separatorChar + justFileName);
uploadedFile.write(file);
}
catch (Exception ex)
{
error("Cannot upload file: " + justFileName);
}
returnnull;
}
But unfortunetly ... it's not working.
"com.sun.rave.web.ui.appbase.ApplicationException: javax.servlet.ServletException: javax.servlet.jsp.JspException: null" caused by "fileUpload.setUploadedFile(uploadedFile);"
Add to that (maybe it'll you find a solution to my problem) :
In the init method,UploadedFile uploadedFile = this.getSessionBean1().getUploadedFile(); always return an instance of a UploadedFile which is never null (which is a mystery beyond our understanding since in the session bean it is initialized to null).
If you uncoment the two comment in the init(), the first println return "uploadedFile= com.sun.rave.web.ui.component.Upload$UploadedFileImpl@6a401e" (which is normal) and the second crash (com.sun.rave.web.ui.appbase.ApplicationException
).
So I'll be very gratefull if someone can solve that.

