[jboss-user] [JBoss Seam] - Re: Simplest <s:fileUpload> usage

nathandennis do-not-reply at jboss.com
Tue Jun 5 00:42:06 EDT 2007


I?m very glad to see the topic of s:upload being discussed, because I too am having problems. I have managed to get s:upload to do exactly what I want with just one file upload box on a page. But my goal is multiple file upload boxes on the same page. What im doing is linking the data and fileName attributes directly to an entity bean that contains my getters and setters. In the getter method for a the column in my db where I want to store the name of each filename I added some code to write the file to a directory on my server then return the file name. pretty simple I thought. But running to problems I  don?t understand 
1.	the only name I can give the fileName attribute is fileName? I know,,, sounds bazaar. But it seems to be the case
2.	if I add the second upload box with different targets, the first box fileName will persist fine,, but and a blank file is written to disk,,, but content is null.
Any help or criticism  will be greatly appreciated.
Using seam 1.2.1  and jboss 4.0.5

  | 
  | <a:outputPanel id="loadresults1">
  | 	 <s:decorate id="photo1" template="layout/edit.xhtml">
  | 		<ui:define name="label">Photo 1:</ui:define>
  | 		<s:fileUpload data="#{inventoryHome.instance.picture}"
  | 			fileName="#{inventoryHome.instance.fileName}"
  |  accept="" />
  | 		<h:commandButton id="buttonpic1" value="Upload"
  | 		action="#{inventoryHome.update}"
  | 		rendered="#{inventoryHome.managed}" reRender="loadresults1">
  | 		<a:support event="onkeyup"
  | 		      actionListener="#{inventoryHome.update}"
  | 			reRender="loadresults1" />
  | 
  | 		</h:commandButton>
  | 	<h:commandButton id="delete" value="Delete"
  | 		action="#{inventoryHome.instance.setPhoto1(null)}"
  | 		rendered="#{inventoryHome.managed}"/>
  | 	</s:decorate>
  | 
  | 	<h:graphicImage id="picture"
  | 		value="/upLoads/#{inventoryHome.instance.photo1}" />
  | </a:outputPanel>
  | 
  | <a:outputPanel>
  | 
  | 	<s:decorate id="photo2" template="layout/edit.xhtml">
  | 		<ui:define name="label">Photo 2:</ui:define>
  | 			<s:fileUpload data="#{inventoryHome.instance.picture2}"
  | 			fileName="#{inventoryHome.instance.fileName2}" accept="" />
  | 			<h:commandButton id="buttonpic2" value="Upload"
  | 				action="#{inventoryHome.update}"
  | 				rendered="#{inventoryHome.managed}" reRender="loadresults1">
  | 		<a:support event="onkeyup"
  | 			actionListener="#{inventoryHome.update}"
  | 				reRender="loadresults2" />
  | 				</h:commandButton>
  | 		<h:commandButton id="delete2" value="Delete"
  | 		action="#{inventoryHome.instance.setPhoto2(null)}"
  | 			rendered="#{inventoryHome.managed}"/>
  | 									
  | 		</s:decorate>
  | 			<h:graphicImage id="picture2"
  | 		value="/upLoads/#{inventoryHome.instance.photo2}" />
  | </a:outputPanel>
  | 
  | 
  | 
  | --------------------------
  | entity bean
  | 
  | 
  | 	@Column(name = "photo1", length = 30)
  | 	@Length(max = 30)
  | 	public String getPhoto1() {
  | 		try{
  | //			first create dir for file - not needed ofcourse
  | 				File os = new 
  | 				file("/usr/local/jboss/server/default/deploy/foo.ear/" +
  | 						"foo.war/upLoads");
  | 			        os.mkdirs();
  | //			create empty file with specified name and path
  | 			      os = new file("/usr/local/jboss/server/default/deploy/" +
  | 			        	"foo.ear/" +
  | 						"foo.war/upLoads/"+getFileName());
  | 			        FileOutputStream oStream =new FileOutputStream(os);
  | 			        oStream.write(getPicture());
  | 			        this.photo1=getFileName();
  | //			        this.picture=null;
  | 			}catch(Exception e){
  | //			  e.printStackTrace();
  | 			}
  | 	
  | 		return this.photo1;
  | 	}
  | 
  | 	public void setPhoto1(String photo1) {
  | 		this.photo1 = photo1;
  | 	}
  | 
  | 	@Column(name = "photo2", length = 30)
  | 	@Length(max = 30)
  | 	public String getPhoto2() {
  | 		try{
  | //			create empty file with specified name and path
  | 			        
  | 			    File os2 = new File("/usr/local/jboss/server/default/deploy/" +
  | 			        	"foo.ear/" +
  | 						"crookmotors.war/upLoads/"+getFileName2());
  | 			        FileOutputStream oStream =new FileOutputStream(os2);
  | 			        oStream.write(getPicture2());
  | 			        this.photo2=getFileName2();
  | //			        this.picture=null;
  | 			}catch(Exception e){
  | //			  e.printStackTrace();
  | 			}
  | 		
  | 		return this.photo2;
  | 	}
  | 
  | 	public void setPhoto2(String photo2) {
  | 		this.photo2 = photo2;
  | 	}
  | 
  | 	public byte [] getPicture() {
  | 		return this.picture;
  | 	}
  | 
  | 	public void setPicture(byte [] picture) {
  | 		this.picture = picture;
  | 	}
  | 	
  | 	public byte [] getPicture2() {
  | 		return this.picture2;
  | 	}
  | 
  | 	public void setPicture2(byte [] picture2) {
  | 		this.picture2 = picture2;
  | 
  | 	public String getFileName() {
  | 		return this.fileName;
  | 	}
  | 
  | 	public void setFileName(String fileName) {
  | 		this.fileName = fileName;
  | 	}
  | 	
  | 	public String getFileName2() {
  | 		return this.fileName;
  | 	}
  | 
  | 	public void setFileName2(String filename2) {
  | 		this.filename2 = filename2;
  | ...
  | 

InventoryHome is an unaltered session bean from seam gen


what am i missing??? 

like i said,, this worked fine will only one field.



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4051164#4051164

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4051164



More information about the jboss-user mailing list