Board index JavaServer Faces General file upload listener not call in primefaces 3.0M1

file upload listener not call in primefaces 3.0M1

Components, Ajax Framework, Utilities and More.


Posts: 59
hi all
I used primefaces 2.2.1 and file upload component worked successfully.
I use this libraries for upload:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
commons-el.jar

and my code :
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{upload2.handleFileUpload2}" allowTypes="*.*;"/>
</h:form>

my fileuypload listener call and work fine but when I removed primefaces-2.2.1.jar file and add primefaces-3.0.M1.jar to my project, file upload listener not call.
can any body help me to solve this problem??

netbeans 6.9, jsf 2.0, glassfish 3.0


Posts: 61
show us how you are using using the fileupload component and the corresponding action handler in the bean. I'm using the upload component so may spot something
Primefaces version: 3.0-SNAPSHOT

JSF implementation: Mojarra 2.0.4

Server: Glassfish 3.1-beta


Posts: 59
Thanks for your attention.
my xhtml page:

<h:body>
        <f:view>
            <h:form enctype="multipart/form-data">
                <p:fileUpload fileUploadListener="#{upload2.handleFileUpload2}" allowTypes="*.*;"/>
            </h:form>
        </f:view>
    </h:body>



bean code to remove uploaded file to the folder named "upload" :

 public void handleFileUpload2(FileUploadEvent event) {
        file = event.getFile();

        String fileName = tempUploadFileFolder + file.getFileName();

        File mailFile = new File(fileName);

        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        String newFileName = servletContext.getRealPath("") + File.separator + "upload" + File.separator + file.getFileName();
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            FileOutputStream fos = new FileOutputStream(new File(newFileName));
            InputStream is = file.getInputstream();
            int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];
            int a;
            while (true) {
                a = is.read(buffer);
                if (a < 0) {
                    break;
                }
                fos.write(buffer, 0, a);
                fos.flush();
            }
            fos.close();
            is.close();
        } catch (IOException e) {
        }
    }


web.xml page:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.PUBLIC_CAPTCHA_KEY</param-name>
        <param-value>6Ld7pMESAAAAAHd1VihJkqPUXAJVwU3Cghc8fzrq</param-value>
    </context-param>

    <context-param>
        <param-name>primefaces.PRIVATE_CAPTCHA_KEY</param-name>
        <param-value>6Ld7pMESAAAAAMhr5WSk5bcRrff8Y08NtDi8Buoq</param-value>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>Resource Servlet</servlet-name>
        <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/primefaces_resource/*</url-pattern>
    </servlet-mapping>

    <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>512000</param-value>
        </init-param>
        <init-param>
            <param-name>uploadDirectory</param-name>
            <param-value>/upload</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/upload2.xhtml</welcome-file>
    </welcome-file-list>

</web-app>



all of this work fine in primefaces 2.2.1 but not in 3.0M1


Posts: 61
Nothing that obvious to me. What I can say is limit the allowtypes. It is probably not expecting a regex but a comma seperated list as in the examples.

also specify a mode as it looks like its required.
This works for me

                    <p:fileUpload fileUploadListener="#{drivingSchoolManager.handleFileUpload}"
                                  mode="advanced"
                                  uploadLabel="#{i18n['upload']}"
                                  update="pnl messages text"
                                  sizeLimit="1000000"
                                  oncomplete=""
                                  allowTypes="png,gif,jpg" >
                       
                    </p:fileUpload>


and this is my abbreviated handler
    public void handleFileUpload( FileUploadEvent event )
    {
        try
        {
            FacesMessage msg = new FacesMessage( "Succesful", event.getFile().
                    getFileName() + " is uploaded." );
            UploadedFile file = event.getFile();

        }
        catch ( Exception ex )
        {
            log.error( ex.toString(), ex );
            JsfUtil.addErrorMessage( ResourceBundle.getBundle( "/i18n" ).
                    getString( "data.save.failed" ) );
            throw new FacesException( ex );
        }
    }
Primefaces version: 3.0-SNAPSHOT

JSF implementation: Mojarra 2.0.4

Server: Glassfish 3.1-beta


Posts: 59
I used your p:fileupload but have not been working.
do you test your code with primefaces 3.0M1?


Posts: 61
Yes, 'm using
<!--            <version>3.0-SNAPSHOT</version>-->
            <version>3.0.M1</version>
Primefaces version: 3.0-SNAPSHOT

JSF implementation: Mojarra 2.0.4

Server: Glassfish 3.1-beta


Posts: 21
Did you get this to work?

I cannot get my listener to work.

I'm also using seam 3, would that have an effect on this?

Thanks

Craig


Posts: 241
I have the same problem.
Can anybody help us?
Primefaces 4.0, JSF 2.1.24, JBoss 7.1


Posts: 61
Well, I'm using mojarra so don't know if that makes a difference but it works fine for me. Maybe Seam is not resolving the method expression properly
Primefaces version: 3.0-SNAPSHOT

JSF implementation: Mojarra 2.0.4

Server: Glassfish 3.1-beta


Posts: 59
in file upload simple mode, my listener called and worked successfully.
this is my xhtml code:
<h:form enctype="multipart/form-data">

            <p:messages showDetail="true"/>

            <p:fileUpload value="#{upload2.file2}" mode="simple" allowTypes="*.*"/>

            <p:commandButton value="Submit" ajax="false"
                             action="#{upload2.testSimpleUpload()}"/>

        </h:form>


and my bean code(i create "upload" folder in web folder to restore uploade files there):
@ManagedBean
@RequestScoped
public class upload2 {

    /** Creates a new instance of upload2 */
    public upload2() {
    }
    private UploadedFile file2;

    public UploadedFile getFile2() {
        return file2;
    }

    public void setFile2(UploadedFile file2) {
        this.file2 = file2;
    }


    public void testSimpleUpload()
    {



        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        String newFileName = servletContext.getRealPath("") + File.separator + "upload" + File.separator + file2.getFileName();
//        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
       // FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            FileOutputStream fos = new FileOutputStream(new File(newFileName));
            InputStream is = file2.getInputstream();
            int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];
            int a;
            while (true) {
                a = is.read(buffer);
                if (a < 0) {
                    break;
                }
                fos.write(buffer, 0, a);
                fos.flush();
            }
            fos.close();
            is.close();
        } catch (IOException e) {
        }
    }

}


but I have same problem in advanced mode.

Next

Return to General