8

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mitra
  • 154
  • 1
  • 1
  • 10

3 Answers3

9

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

Community
  • 1
  • 1
MRX
  • 1,611
  • 8
  • 33
  • 55
  • NO luck Kshitij ive added the following jars commons-io-2.4.jar, commons-io-2.4-sources.jar,commons-fileupload-1.2.2.jar along with the prime faces and also updated web.xml based on your code Also I cannot see any stack trace to atleast debug it is there a way to print it – Mitra Nov 20 '12 at 11:52
  • 1
    The filter mapping is missing from this answer. Have you added it? It's all in detail described in PrimeFaces user's guide. See also http://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked/8880083#8880083 – BalusC Nov 20 '12 at 12:20
  • Thank you balus it worked after adding filter mapping also Thank you very much – Mitra Nov 21 '12 at 04:54
1

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/

Aksel Willgert
  • 11,367
  • 5
  • 53
  • 74
  • Hello Aksel ive updated the post ive tried with managed and session still doesnt change a bit and are there any dependencies that i have to add apart from core JSF i have added only primefaces 3.4.2 jar – Mitra Nov 20 '12 at 11:10
1

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.

Jorge
  • 11
  • 1