Help

Controls

PermLinkWikiLink

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

Forum: Seam Users Forum ListTopic List
22. Aug 2008, 01:11 CET | Link

Hi, I need to upload images files, I try <s:fileUpload> component, and it works fine for me, but every example I find, saves the file to database and I need to do it into a web directory. Is this possible? Can somebody show me an example?

thanks in advance

2 Replies:
05. Sep 2008, 21:42 CET | Link

Excuse me, could perform what you Wait? If so, you could put your SOLUTION, PLEASE? Is that I am having the same problem

Thank you

06. Sep 2008, 22:10 CET | Link

This work for me


@Name("uploadHandler")
@Scope(ScopeType.EVENT)
public class UploadHandler {
		
	@In(value="#{facesContext}")
	FacesContext facesContext;
	
	@Logger private Log log;
	
	
	private byte[] file;	 
	
	public void handleUpload() throws IOException{
		
		String path = ((ServletContext) facesContext.getExternalContext().getContext()).getRealPath("/my/files/dir");
		File f = new File(path,"myFile.txt");
		log.info(f);
		FileOutputStream fo = new FileOutputStream(f);
		fo.write(file);
		fo.flush();
		fo.close();
	
	}

	public byte[] getFile() {
		return file;
	}

	public void setFile(byte[] file) {
		this.file = file;
	}

}

and the form


<h:form id="fooForm" enctype="multipart/form-data">
        
           <s:fileUpload id="file" 
				data="#{uploadHandler.file}"
				accept="image/jpg"
		    />
        
            <h:commandButton id="upload" value="Upload !!" 
                             action="#{uploadHandler.handleUpload}"/>     			  
        
        </h:form>

But I don't recommand to use the ....getRealPath(/my/files/dir);.... instead use a path that you configure in seam.properties.