[SOLVED] FileUpload executes twice

UI Components for JSF
Post Reply
ricardo13
Posts: 9
Joined: 18 Nov 2010, 14:30

23 Nov 2010, 15:48

Hi,

I have a screen that the system updates data's user. This screen may add images (or update your photo), so there are two buttons. One button to upload file and another called UPDATE button to update data person in a DataBase.

Firstly, when I upload a file it works fine.
After I click UPDATE button and the fileupload method executes again and all data about user are lost and nothing is updated in DB.

Any idea ?

Thank you
Ricardo
Last edited by ricardo13 on 24 Nov 2010, 14:42, edited 1 time in total.

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

23 Nov 2010, 16:39

Hi,

You should post your code here. It's difficult to see what is wrong w/o code.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

ricardo13
Posts: 9
Joined: 18 Nov 2010, 14:30

23 Nov 2010, 17:28

My page update_User.xhtml

Code: Select all

<h:form  id="form" prependId="false" enctype="multipart/form-data" >		
                <!-- Fields of form -->
		...
		<p:fileUpload 	allowTypes="*.jpg;*.png;*.gif;"
     						auto="false"     						
     						description="Image"
     						fileUploadListener="#{userMB.uploadFile}"
     						label="Select File"
     						multiple="false"   						
     		/>    					
     		<br/>
     		<h:commandLink value="Edit" action="#{userMB.updateUser}" /><br/>			
			<h:messages showDetail="true" showSummary="true" />     		
     		     	
    	</h:form>
My Managed-Bean

Code: Select all

@Scope("session")
public class UsuarioMB implements Serializable {

	private User user;
	private String photoName;	
        
        /* Getters and Setters */

        public String updateUser() {
	
		System.out.println("Updating ...");			
		try {			
			this.setUser(userComponent.modifyUser(user));			
			return "sucess";
			
		} catch(BusinessException e) {
			return "error";
		}
	}

        public void uploadFile(FileUploadEvent event) {	
				
		byte[] foto = event.getFile().getContents();
		String fileName = event.getFile().getFileName();
		
		ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();		
		String filePath = servletContext.getRealPath("\\WEB-INF\\classes\\upload") + "\\photo\\";
			
		this.getUser().setPhoto(fileName);
		
		try {
                        System.out.println("OUTPUT: " + this.getUser().getPhoto);
			createFile(filePath, fileName, photo);
		} catch(FileNotFoundException ex) {
			ex.printStackTrace();			
		} catch(IOException ex) {
			ex.printStackTrace();
		}
	}
	
	private void createFile(String path, String name, byte[] photo) throws IOException {
		FileOutputStream fos = new FileOutputStream(new File(path+name));
		fos.write(photo);	
		fos.close();		
	}
When i click on UPLOAD link the console shows:
OUTPUT: image.jpg

Now, When I click on EDIT button the console shows:
OUTPUT:
Updating ...
Hibernate: update user .....

I hope it helps you

thank you
Ricardo

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

23 Nov 2010, 22:26

Hi,

I don't know, it seems to be a bug with fileUpload. Try this please

Code: Select all

<p:commandLink value="Edit" action="#{userMB.updateUser}" process="@this" />
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

24 Nov 2010, 00:31

Can you also try with removing enctype="multipart/form-data"?

ricardo13
Posts: 9
Joined: 18 Nov 2010, 14:30

24 Nov 2010, 12:14

I'm using the version Primeface 1.1

Thank you
Ricardo

ricardo13
Posts: 9
Joined: 18 Nov 2010, 14:30

24 Nov 2010, 14:42

I'm saved !!!

Code: Select all

<p:commandLink value="Edit" action="#{userMB.updateUser}" process="@this" />
Thank you!!
Ricardo

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

25 Nov 2010, 11:03

Hi,

Yes, how I said it seems to be a bug in p:fileUpload. fileUploadListener="#{userMB.uploadFile}" should not be executed if fileUpload inside of "process" area.

I think, we need an issue.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 46 guests