Can't upload files by rich:fileUpload
kirkor.grzegorz.bernas.com.pl Nov 18, 2008 9:29 AMI'm trying to upload files. But without any errors I cant't. I use Seam 2.0.2 SP1 and I add newest RF lib 3.2.2.SR1
Do I need to ad something to my components.xml or web.xml?
My bean:
`@AutoCreate
@Name("fileUpload")
@Scope(ScopeType.SESSION)
public class FileUpload {
@Logger
Log log;
private ArrayList<PersonFile> files = new ArrayList<PersonFile>();
public int getSize() {
log.info("size: " + getFiles().size());
if (getFiles().size() > 0) {
return getFiles().size();
} else {
return 0;
}
}
public void listener(UploadEvent event) throws IOException {
UploadItem item = event.getUploadItem();
PersonFile pfile = new PersonFile();
pfile.setFilename(item.getFileName());
files.add(pfile);
System.out.println("File : '" + item.getFileName() + "' was uploaded");
if (item.isTempFile()) {
File file = item.getFile();
System.out.println("Absolute Path : '" + file.getAbsolutePath() + "'!");
file.delete();
}else {
try {
ByteArrayOutputStream b = new ByteArrayOutputStream();
b.write(item.getData());
System.out.println(b.toString());
} catch (Exception e) {
// TODO: handle exception
}
}
}
public ArrayList<PersonFile> getFiles() {
return files;
}
public void setFiles(ArrayList<PersonFile> files) {
this.files = files;
}
}`
My view:
`<h:form>
<h:panelGrid columns="2" columnClasses="top,top">
<rich:fileUpload fileUploadListener="#{fileUpload.listener}" maxFilesQuantity="5" reRender="table" id="upload"
immediateUpload="false" acceptedTypes="*" allowFlash="false" uploadData="#{fileUpload.files}">
<a4j:support event="onuploadcomplete" reRender="info" />
</rich:fileUpload>
<h:panelGroup id="info">
<rich:panel bodyClass="info">
<f:facet name="header">
<h:outputText value="Uploaded Files Info" />
</f:facet>
<h:outputText value="No files currently uploaded" rendered="#{fileUpload.size==0}" />
<rich:dataGrid columns="1" value="#{fileUpload.files}" var="file" rowKeyVar="row">
<rich:panel bodyClass="rich-laguna-panel-no-header">
<h:panelGrid columns="2">
<h:panelGrid columns="2">
<h:outputText value="File Name:" />
<h:outputText value="#{file.filename}" />
</h:panelGrid>
</h:panelGrid>
</rich:panel>
</rich:dataGrid>
</rich:panel>
<rich:spacer height="3" />
<br />
</h:panelGroup>
</h:panelGrid>
</h:form>`
Do I need to ad something to my components.xml or web.xml?
My bean:
`@AutoCreate
@Name("fileUpload")
@Scope(ScopeType.SESSION)
public class FileUpload {
@Logger
Log log;
private ArrayList<PersonFile> files = new ArrayList<PersonFile>();
public int getSize() {
log.info("size: " + getFiles().size());
if (getFiles().size() > 0) {
return getFiles().size();
} else {
return 0;
}
}
public void listener(UploadEvent event) throws IOException {
UploadItem item = event.getUploadItem();
PersonFile pfile = new PersonFile();
pfile.setFilename(item.getFileName());
files.add(pfile);
System.out.println("File : '" + item.getFileName() + "' was uploaded");
if (item.isTempFile()) {
File file = item.getFile();
System.out.println("Absolute Path : '" + file.getAbsolutePath() + "'!");
file.delete();
}else {
try {
ByteArrayOutputStream b = new ByteArrayOutputStream();
b.write(item.getData());
System.out.println(b.toString());
} catch (Exception e) {
// TODO: handle exception
}
}
}
public ArrayList<PersonFile> getFiles() {
return files;
}
public void setFiles(ArrayList<PersonFile> files) {
this.files = files;
}
}`
My view:
`<h:form>
<h:panelGrid columns="2" columnClasses="top,top">
<rich:fileUpload fileUploadListener="#{fileUpload.listener}" maxFilesQuantity="5" reRender="table" id="upload"
immediateUpload="false" acceptedTypes="*" allowFlash="false" uploadData="#{fileUpload.files}">
<a4j:support event="onuploadcomplete" reRender="info" />
</rich:fileUpload>
<h:panelGroup id="info">
<rich:panel bodyClass="info">
<f:facet name="header">
<h:outputText value="Uploaded Files Info" />
</f:facet>
<h:outputText value="No files currently uploaded" rendered="#{fileUpload.size==0}" />
<rich:dataGrid columns="1" value="#{fileUpload.files}" var="file" rowKeyVar="row">
<rich:panel bodyClass="rich-laguna-panel-no-header">
<h:panelGrid columns="2">
<h:panelGrid columns="2">
<h:outputText value="File Name:" />
<h:outputText value="#{file.filename}" />
</h:panelGrid>
</h:panelGrid>
</rich:panel>
</rich:dataGrid>
</rich:panel>
<rich:spacer height="3" />
<br />
</h:panelGroup>
</h:panelGrid>
</h:form>`