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
[SOLVED] FileUpload executes twice
Hi,
You should post your code here. It's difficult to see what is wrong w/o code.
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
My page update_User.xhtml
My Managed-Bean
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
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>
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();
}
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
Hi,
I don't know, it seems to be a bug with fileUpload. Try this please
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
-
- Prime
- Posts: 18616
- Joined: 05 Jan 2009, 00:21
- Location: Cybertron
- Contact:
Can you also try with removing enctype="multipart/form-data"?
I'm saved !!!
Thank you!!
Ricardo
Code: Select all
<p:commandLink value="Edit" action="#{userMB.updateUser}" process="@this" />
Ricardo
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.
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
-
- Information
-
Who is online
Users browsing this forum: No registered users and 46 guests