1 2 Previous Next 22 Replies Latest reply on Nov 19, 2012 3:08 PM by alpamys.kanibetov Go to original post
      • 15. Re: s:fileUpload
        gbc1

        Coming from Seam 2.0 CR2, the following works for me:


        Example:


        <h:form id="form" enctype="multipart/form-data">
             <h3>
                  Upload file
             </h3>
             Select a PDF file for upload<br />
             <s:fileUpload id="file" data="#{myAction.pdf}" accept="application/pdf" />
             <h:commandButton id="show" action="#{myAction.show()}" value="Go" />
        </h:form>
        


        From components.xml:


        <component class="org.jboss.seam.web.MultipartFilter">
          <property name="createTempFiles">true</property>
          <property name="maxRequestSize">10000000</property>
        </component>



        The action method show() must check the pdf on null, because it is invoked when the page loads and when the button is been pressed, as far as I know. Rest should be simple...


        Greetz,


        GHad



        • 16. Re: s:fileUpload
          shane.bryzak

          Are you using any other filters?  Are you using Myfaces, Trinidad, etc?  What is the actual type of the request object in FileUploadRendererBase.doDecode()?  You need to actually spend some time debugging this method to diagnose what's wrong, the fact that it goes into the if test doesn't really help.

          • 17. Re: s:fileUpload
            lasansue.jeremy.girard.satives.fr

            After some debugging, the parameters map of multipart request is empty so the method getFileInputStream, getFileName, ... always return null.


            I use no special filter.

            • 18. Re: s:fileUpload
              lasansue.jeremy.girard.satives.fr

              Get it.
              The problem was that i had trinidad jar in my web-inf/lib.
              So the fileUpload wrapper was handle by trinidad.


              I see some post with seam + trinidad working together, but in my case it's not working.


              Thank you all for your contribution.

              • 19. Re: s:fileUpload
                markusm

                Hi all,


                I still have a similar problem (JBoss 4.2.2, Seam 2.0.1, Richfaces 3.2.0) My byte-array is always null:


                web.xml


                <?xml version="1.0" encoding="UTF-8"?>
                <web-app version="2.5" 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_2_5.xsd">
                     <session-config>
                          <session-timeout>
                               30
                          </session-timeout>
                     </session-config>
                     <welcome-file-list>
                          <welcome-file>index.xhtml</welcome-file>
                     </welcome-file-list>
                
                     <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>*.xhtml</url-pattern>
                     </servlet-mapping>
                     <listener>
                          <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
                     </listener>
                
                     <context-param>
                          <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
                          <param-value>.xhtml</param-value>
                     </context-param>
                
                     <servlet>
                          <servlet-name>Seam Resource Servlet</servlet-name>
                          <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
                     </servlet>
                
                     <servlet-mapping>
                          <servlet-name>Seam Resource Servlet</servlet-name>
                          <url-pattern>/seam/resource/*</url-pattern>
                     </servlet-mapping>
                
                     <filter>
                          <filter-name>Seam Filter</filter-name>
                          <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
                     </filter>
                
                     <filter-mapping>
                          <filter-name>Seam Filter</filter-name>
                          <url-pattern>/*</url-pattern>
                     </filter-mapping>
                
                     <context-param>
                          <param-name>org.richfaces.SKIN</param-name>
                          <param-value>blueSky</param-value>
                     </context-param>
                
                     <filter>
                          <display-name>RichFaces Filter</display-name>
                          <filter-name>richfaces</filter-name>
                          <filter-class>org.ajax4jsf.Filter</filter-class>
                     </filter>
                
                     <filter-mapping>
                          <filter-name>richfaces</filter-name>
                          <servlet-name>Faces Servlet</servlet-name>
                          <dispatcher>REQUEST</dispatcher>
                          <dispatcher>FORWARD</dispatcher>
                          <dispatcher>INCLUDE</dispatcher>
                     </filter-mapping>
                </web-app>



                components.xml:


                <component class="org.jboss.seam.web.MultipartFilter">
                          <property name="createTempFiles">true</property>
                          <property name="maxRequestSize">1000000</property>
                     </component>





                on my xhtml page :


                <a4j:form enctype="multipart/form-data">
                     <s:fileUpload data="#{currentCompany.logo}" accept="gif,jpg,jpeg,png"/>
                     <a4j:commandButton type="submit"/>
                </a4j:form>


                currentCompany is my conversation-scoped Entity, but setLogo is always called with null.


                Any ideas?

                • 20. Re: s:fileUpload
                  markusm

                  Made a mistake, i am already using Seam 2.0.2.GA

                  • 21. Re: s:fileUpload
                    markusm

                    It works with h:commandButton instead of a4j:commandButton

                    • 22. Re: s:fileUpload
                      alpamys.kanibetov

                      JBoss, Seam.

                      Hi, it saves in temporary folder(in Linux it is /tmp) if you specify createTempFiles to true, make it false, then the file will be stored in RAM, those you can get file in array byte[](It won't be null).

                       

                      web.xml:

                      <filter>
                            <filter-name>Seam Filter</filter-name>
                            <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
                            <init-param>
                                 <param-name>createTempFiles</param-name>
                                 <param-value>false</param-value>
                            </init-param>
                            <init-param>
                                 <param-name>maxRequestSize</param-name>
                                 <param-value>1024000</param-value>
                            </init-param>
                      </filter>
                      

                       

                      components.xml:(maybe you can skip this part)

                      <web:multipart-filter create-temp-files="false"
                                            max-request-size="1024000" url-pattern="*.seam"/>
                      

                       

                      fileupload.xhtml:

                      <html xmlns="http://www.w3.org/1999/xhtml"
                                xmlns:h="http://java.sun.com/jsf/html"
                                xmlns:s="http://jboss.com/products/seam/taglib">
                      
                      
                                <h:form enctype="multipart/form-data">
                                          <s:fileUpload data="#{fileService.file}" fileName="#{fileService.fileName}" contentType="#{fileService.fileContentType}" accept="image/png"/>
                                          <h:commandButton value="Upload" actionListener="#{fileService.saveFile}" type="submit"/>
                                </h:form>
                      </html>
                      

                       

                       

                      FileService.java:

                      package org.helloworld.action.seam;
                      
                      import java.io.File;
                      import java.io.FileOutputStream;
                      import java.io.IOException;
                      import java.io.Serializable;
                      
                      import org.jboss.seam.ScopeType;
                      import org.jboss.seam.annotations.Name;
                      import org.jboss.seam.annotations.Scope; 
                      
                      @Name("fileService")
                      @Scope(ScopeType.CONVERSATION)
                      public class FileService implements Serializable
                      {
                                private byte[] file;
                                private String fileContentType;
                                private String fileName;
                      
                                public byte[] getFile() {
                                          return file;
                                }
                                public void setFile(byte[] file) {
                                          this.file = file;
                                }
                      
                                public String getFileContentType() {
                                          return fileContentType;
                                }
                      
                                public void setFileContentType(String fileContentType) {
                                          this.fileContentType = fileContentType;
                                }
                      
                                public String getFileName() {
                                          return fileName;
                                }
                      
                                public void setFileName(String fileName) {
                                          this.fileName = fileName;
                                }
                      
                                public void saveFile() throws IOException
                                {
                                          FileOutputStream out = new FileOutputStream( new File("/home/alpamys/store/" + fileName) );
                                          out.write( file, 0, file.length );
                                }
                      }
                      

                       

                       

                      If you use RichFaces, there's also the same approach(createTempFiles is false)

                       

                      Here are bean and view:

                      FileUploadBean.java:

                      package org.helloworld.action.richinput;
                      
                      import java.io.File;
                      import java.io.FileOutputStream;
                      import java.io.IOException;
                      import java.io.Serializable;
                      
                      import org.jboss.seam.ScopeType;
                      import org.jboss.seam.annotations.Name;
                      import org.jboss.seam.annotations.Scope;
                      import org.richfaces.event.UploadEvent;
                      import org.richfaces.model.UploadItem;
                      
                      @Name("fileUploadBean")
                      @Scope(ScopeType.SESSION)
                      public class FileUploadBean implements Serializable
                      {
                        private static final long serialVersionUID = -3685074215794633251L;
                      
                          public FileUploadBean() {}
                      
                          public synchronized void listener(UploadEvent event) throws Exception
                          {
                              UploadItem item = event.getUploadItem();
                              
                              if (item != null && item.getData() == null) // stores file in temp directory
                              {
                                     // some code
                              }
                      
                              if (item != null && item.getData() != null) // stores file in RAM
                              {
                                 FileOutputStream out = new FileOutputStream( new File("/home/alpamys/store/" + item.getFileName()) );
                                 out.write( item.getData(), 0, item.getData().length );
                              }
                          }
                      }
                      

                       

                      fileupload.xhtml:

                      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                          xmlns:s="http://jboss.com/products/seam/taglib"
                          xmlns:ui="http://java.sun.com/jsf/facelets"
                          xmlns:f="http://java.sun.com/jsf/core"
                          xmlns:rich="http://richfaces.org/rich"
                          xmlns:h="http://java.sun.com/jsf/html"
                                xmlns:c="http://java.sun.com/jstl/core"
                                xmlns:a="http://richfaces.org/a4j">
                      
                                   <h:form class="fileUpload" id="fileUploadForm">
                                            <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}" immediateUpload="true" 
                                                                   acceptedTypes="jpg, gif, png, bmp" allowFlash="false" autoclear="true">
                                            </rich:fileUpload>
                                   </h:form>
                      
                      </ui:composition>
                      

                       

                      Link to demo: http://livedemo.exadel.com/richfaces-demo/richfaces/fileUpload.jsf?c=fileUpload&tab=usage

                      1 2 Previous Next