I'm trying to upload file to the server using rich:fileUpload, here's the code:
@Name("fileUploader")
public class FileUploader {
private byte[] fileData;
public void uploadFileListener(UploadEvent uploadEvent) {
fileData = uploadEvent.getUploadItem().getData();
//other code here
}
}
page contains the following :
rich:fileUpload fileUploadListener="#{fileUploader.uploadFileListener}"
What I'm getting is that uploadEvent contains data about file name, size and so on... but
uploadEvent.getUploadItem().getData();
returns null...
I've already seen similar issue here... but there's no answer...
Thanks!
Let's put this in order:
First at all you have to define if you want your data uploaded directly in a
byte[]
buffer or in a temporary file and the maximum size supported. That is why you have to define two parameters in some place.Now the place where you need to define those parameters, depends on which component you are using (i.e. seam fileUpload component or richfaces fileUpload component) and which technology is mounted for your application. Let's see:
If you are using seam framework with richfaces and you don't need ajax support (i.e. show the image being loaded once upload is finished),
<s:fileUpload>
would be a good choice, to configure it you have to change your seamcomponents.xml
file this way:This means that the data you send to the server will be saved in a temporary file and that the maximum size supported is
1000000
(about 1M). More info can be found here: http://seamframework.org/Documentation/HowToUploadAndDownloadFilesInSeamNow and using the same technology stack (seam + richfaces) but in this case you need to use ajax support.
<rich:fileUpload>
will be in that case your choice. To configure it just change this in the Seam Filter defined inweb.xml
file:Note that although the parameters are the same in both cases they are defined in different places -I imagine this is changed in JEE6 with CDDI-.
If your application don't use seam framework but it does use richfaces probably you have to define this parameters it in the corresponding richfaces filter again in
web.xml
.Now the interesting part, the usage of
<rich:fileUpload>
. This is the relevant part of a file calledBlockEdit.xhtml
where the component is used:And this is the fileUploadListener of
BlockHome.java
If your configuration of
createTempFiles
inside the seam filter in web.xml was:then item.getData() will be not null. But if you defined the parameter
createTempFiles
totrue
, then you should ask for item.getFile() if you want to know if it has a value. This is because the item.getData() is filled just when you decide to avoid the creation of temporary files, as stated in Richfaces demo fileUpload documentation.Hope this helps
Did you configured multipart-filter in components xml?
Something like this:
Update: I'm not completely sure if this is used for s:fileUpload or rich:fileUpload. Please check the code bellow before thinking about this configuration. By default, if I remember correctly, you should be using temp files which is the default configuration for RichFaces I think. Sorry but I don't have my project here to check it.
If you have it configured like the above your file will be saved to a temporary file (create-temp-files="true"), in this case you should access your data by using
You can check if it is stored in a file with:
Since you say that the file "metadata" is there, this looks to be the problem, you are just looking for your data in the wrong place :)
If you have it configured to false, your method should work.
Also I remember something (not completely sure) that the upload control (rich:upload) needed to be inside an h:form
Hope it helps.