Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileDownload and FileUpload JSF Primefaces not working

Tags:

jsf

primefaces

I'm using PrimeFaces 3.1.2, NetBeans 7.2, JSF 2.1 and GlassFish 3.1.2.

I'm using the actual code I got from http://www.primefaces.org/showcase/ui/fileUploadAuto.jsf and http://www.primefaces.org/showcase/ui/fileDownload.jsf.

When I run the file upload code, it doesn't work at all. The file doesn't get uploaded and no success message is shown. But if the file size exceeds the size mentioned, it is showing a message that the file size is too large.

Here is my view:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
        <h:form enctype="multipart/form-data">
            <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
                mode="advanced"
                update="messages"
                auto="true"
                sizeLimit="100000" 
                allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
            <p:growl id="messages" showDetail="true"/>
        </h:form>
    </h:body>
</html>

Here is my backing bean:

package com;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;

@ManagedBean
@SessionScoped
public class FileUploadController {
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful",   event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

Its basically the same code in the PrimeFaces showcase page.

Similarly with file download code; when I click on download nothing happens. A pop up opens and closes before I could even notice it. I have the image file in place as mentioned in the get resource stream (for the download part), but I don't know what's the problem. The code is also basically the same as in the PrimeFaces showcase page.

I don't see any logs or errors under Glassfish in Netbeans. I also don't know how to enable logging if necessary.

like image 343
Mitra Avatar asked Nov 20 '12 10:11

Mitra


3 Answers

The first thing you need is add some libraries to your application. As a matter of fact, PrimeFaces file upload relies on Apache commons-file-upload and commons-io libraries. So dowload them and add them to your WEB-INF/lib path:

you can download it from following link.

http://commons.apache.org/io/

http://commons.apache.org/fileupload/

in addition you have to configure it into web.xml

<filter>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
  <init-param>
    <param-name>thresholdSize</param-name>
    <param-value>51200</param-value>
  </init-param>
  <init-param>
    <param-name>uploadDirectory</param-name>
    <param-value>C:\etc</param-value>
   </init-param>
</filter>
<filter-mapping>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Also If you want to set programmatically change the destination of your uploaded files have a look:

PrimeFaces FileUpload File Saving Destination

like image 90
KSHiTiJ Avatar answered Oct 12 '22 10:10

KSHiTiJ


To be able use the bean from the xhtml, you need to annotate your controller as a @ManagedBean and set some scope. Preferably @ViewScoped or @RequestScoped in this case.

example:

@ManagedBean
@ViewScoped
public class FileUploadController {

    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful",     event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

Learn more about how jsf managed beans works here: http://www.mkyong.com/jsf2/configure-managed-beans-in-jsf-2-0/

like image 33
Aksel Willgert Avatar answered Oct 12 '22 10:10

Aksel Willgert


One thing that I have noticed is that if you don't put inside the tag "allowTypes" a regular expression correctly, the "fileupload" element does not trigger the action, giving the impression that the action is unreachable.

I know that this is not your problem right now, but I think it is important to share this information.

like image 32
Jorge Avatar answered Oct 12 '22 09:10

Jorge