1 2 Previous Next 18 Replies Latest reply on Apr 24, 2008 9:54 PM by Yurii Kartsev

    file upload problems

    Simon Cigoj Newbie

      I'm trying tu use richfaces file upload component with myfaces, and when I upload the file nothing happens...I putted a brakepoint in the listener but it looks like that the listener is not invoked...so the listener does not fire.

      Below I posted my code and the settings of my environment, can anyone help me because this file upload looks realy cool and I would like to use it.

      My code

      public class UploadHandler {
      
       public void fileUploadListener(org.richfaces.event.UploadEvent event) {
       if(event==null){
       System.out.println("null upload event");
       return;
       }
       UploadItem item = event.getUploadItem();
      
       String name = "unnamed_attachment";
       byte[] data = item.getData();
      
       if (item.isFile()) {
       name = item.getFileName();
       data = item.getData();
       File file = item.getFile();
       System.out.println(
       "uploaded "+name+
       " - length= "+( (data==null)?0:data.length) +
       " tmpFile="+file.getAbsoluteFile()
       );
       }
       }
      }
      


      faces file:

      <!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:a4j="http://richfaces.org/a4j"
       xmlns:rich="http://richfaces.org/rich">
      
       <h:form enctype="multipart/form-data">
      
       <rich:fileUpload id="upload" fileUploadListener="#{uploadHandler.fileUploadListener}" maxFilesQuantity="2">
       <f:facet name="label">
       <h:outputText value="{_KB}KB from {KB}KB uploaded {mm}:{ss}" />
       </f:facet>
       </rich:fileUpload>
      
       </h:form>
      
      </html>
      


      my web.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
       <display-name>FileUploadJSF</display-name>
      
       <context-param>
       <param-name>org.ajax4jsf.VIEW_HANDLERS</param-name>
       <param-value>com.sun.facelets.FaceletViewHandler</param-value>
       </context-param>
      
       <context-param>
       <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
       <param-value>.xhtml</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>org.apache.myfaces.USE_ENCRYPTION</param-name>
       <param-value>false</param-value>
       </context-param>
      
       <context-param>
       <param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
       <param-value>false</param-value>
       </context-param>
      
       <context-param>
       <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
       <param-value>false</param-value>
       </context-param>
      
       <listener>
       <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
       </listener>
      
       <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>
      
       <welcome-file-list>
       <welcome-file>index.faces</welcome-file>
       </welcome-file-list>
      
       <context-param>
       <param-name>facelets.LIBRARIES</param-name>
       <param-value>
       /WEB-INF/facelets/customTags/customTags.taglib.xml;
       /WEB-INF/facelets/editView/editView.taglib.xml
       </param-value>
       </context-param>
      
       <!-- Ajax4JSF filter mora biti definiran pred vsemi drugimi filtri -->
       <filter>
       <display-name>Ajax4jsf Filter</display-name>
       <filter-name>ajax4jsf</filter-name>
       <filter-class>org.ajax4jsf.Filter</filter-class>
       <init-param>
       <param-name>forceparser</param-name>
       <param-value>true</param-value>
       </init-param>
      
       <init-param>
       <param-name>maxRequestSize</param-name>
       <param-value>20000000</param-value>
       </init-param>
       </filter>
      
       <filter-mapping>
       <filter-name>ajax4jsf</filter-name>
       <servlet-name>Faces Servlet</servlet-name>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>FORWARD</dispatcher>
       <dispatcher>INCLUDE</dispatcher>
       </filter-mapping>
      
       <filter>
       <filter-name>MyFacesExtensionsFilter</filter-name>
       <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
       </filter>
      
       <!-- extension mapping for serving page-independent resources (javascript, stylesheets, images, etc.) -->
       <filter-mapping>
       <filter-name>MyFacesExtensionsFilter</filter-name>
       <url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
       </filter-mapping>
      
       <!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages -->
       <filter-mapping>
       <filter-name>MyFacesExtensionsFilter</filter-name>
       <servlet-name>Faces Servlet</servlet-name>
       </filter-mapping>
      
       <!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages -->
       <filter-mapping>
       <filter-name>MyFacesExtensionsFilter</filter-name>
       <url-pattern>*.jsf</url-pattern>
       </filter-mapping>
      
      
      </web-app>


      my faces.config.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xi="http://www.w3.org/2001/XInclude"
       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-facesconfig_1_2.xsd">
       <managed-bean>
       <managed-bean-name>
       uploadHandler</managed-bean-name>
       <managed-bean-class>
       upload.UploadHandler</managed-bean-class>
       <managed-bean-scope>
       request</managed-bean-scope>
       </managed-bean>
       <application>
      
       <locale-config>
       <default-locale>en</default-locale>
       </locale-config>
       </application>
      </faces-config>
      


        • 1. Re: file upload problems
          Gianfranco Zola Newbie

          Hi!

          As fas as I know in the UploadListener you have data in getData or in getFile.
          It depends on if you use temp file creation or not.
          I don't see in your Web.XML file byte the Ajax filter the createTempFiles parameter defined. This is required for fileupload.

          If you set the createTempFiles parameter to true, then in the uploadlistener you will get for getData NULL, but for getFile you will get the file for the temp file created. With getFileName you can acquire the original filename.

          If you set the createTempFiles parameter to false, then the whole file will be stored in memory, so with getData you will receive the file content as byte[], and the getFile will return NULL.

          I read before on the forum, that somebody had problems with the temp files, that jboss wont delete them. I faced the problem also, but I had no problem to delete the file from code after processing with File.delete().
          If you use temp files (I think recommended way, except files < 10kb), after processing the file (or storing it elsewhere), you should delete it.

          Other problem I faced with fileupload is, that it seems not to work with request scoped beans, with a4j:keepalive. I receive exception, that UploadItem is not serializable. And if I use a List in the uploadData property of fileUpload, it remains empty.

          Is there any solution except hacking around the sessionmap?

          Regards,

          Zola

          • 2. Re: file upload problems
            Andrei Markavtsov Apprentice

            SimonCigoj,

            I think the problem in filters definition in web.xml.

            To fire file upload under myfaces it necessary to configure invoking filters in the following order:
            1. ajax4jsf,
            2. myfacesExtension.

            I looked on your web.xml.

            I can suggest to remove this mapping from it:

            <!-- extension mapping for adding , , and other resource tags to JSF-pages -->
            <filter-mapping>
            <filter-name>MyFacesExtensionsFilter</filter-name>
            <url-pattern>*.jsf</url-pattern>
            </filter-mapping>


            Please try this solution and let us know if it was helpfull.

            Thanks.

            • 3. Re: file upload problems
              Andrei Markavtsov Apprentice

              gizola,

              Your problem was added in JIRA: http://jira.jboss.com/jira/browse/RF-3065.

              Fixed.

              • 4. Re: file upload problems
                Simon Cigoj Newbie

                Hi

                I putted the

                <init-param>
                 <param-name>createTempFiles</param-name>
                 <param-value>true</param-value>
                 </init-param>


                in web.xm, and I think that the filters are in the right order, I also removed the lines suggested by andrei_exadel, but it still doesn't help, the problem is that it doesnt even arive to the listener...I putted a brakepoint in the first if of the fileUploadListener method and it never arives there after submiting the file.

                So the problem is not solved :((

                Could you guys of exadel write a complete example..also the java code, not only the jsf code in your developer guides.

                • 5. Re: file upload problems
                  Simon Cigoj Newbie

                  does anyone have a working version of the file upload on myfaces1.2 ?

                  if so could you post your code...please

                  • 6. Re: file upload problems
                    Yurii Kartsev Apprentice

                     

                    "SimonCigoj" wrote:
                    ...
                    the problem is that it doesnt even arive to the listener...I putted a brakepoint in the first if of the fileUploadListener method and it never arives there after submiting the file
                    ...

                    Hello, I've got the same problem. Breakpoint isn't reachable and I can't see logs in the output console which is done by "System.out.println". I use RF sample from http://anonsvn.jboss.org/repos/richfaces/trunk/samples/fileUploadDemo.

                    BUT the JSF view shows me the percentage and that the file is uploaded successfully. But there are no signs of that the listener was invoked(like logs or breakpoint stops).

                    AND I have the "createTempFiles" parameter from the web.xml, set to false. And in this case the file should be printed out into console - it doesn't happen.

                    I use RF 3.2.0, JSF 1.2 + Facelets.

                    Thanks for any help in advance!

                    • 7. Re: file upload problems
                      Simon Cigoj Newbie

                      I partiali solved the problem...with using the snapshot 3.2.1 of rich faces http://snapshots.jboss.org/maven2/org/richfaces/ui/richfaces-ui/3.2.1-SNAPSHOT/

                      but I still have problems retreiving the file, the getData is NULL but i think that has been resolved in some other posts

                      So If u upgrade to 3.2.1 the listener should fire...I'll try to store the file soem other day...just too tired today :-)

                      • 8. Re: file upload problems
                        Yurii Kartsev Apprentice

                         

                        "SimonCigoj" wrote:
                        ...
                        upgrade to 3.2.1
                        ...

                        Thanks, I'll try. BUT,using file monitor, I've noticed the thing that the file REALLY was uploaded in the temp dir(defined in Windows environment variables)! But the listener wasn't invoked(because the logs were absent). Strange... Maybe we do something wrong?

                        • 9. Re: file upload problems
                          Andrei Markavtsov Apprentice

                          Ok guys,

                          I'll attach example (RF 3.2.1 + myfaces WAR file) to JIRA to provide you example how it works =).

                          I'll inform you about issue number after I will complete.





                          • 10. Re: file upload problems
                            Andrei Markavtsov Apprentice

                            I created issue. Example application attached there:

                            http://jira.jboss.com/jira/browse/RF-3096

                            Myfaces 1.2
                            Rf 3.2.1

                            Please look at console. File name and temp file's absolute name are logging.

                            • 11. Re: file upload problems
                              Yurii Kartsev Apprentice

                              Andrei, I don't use myfaces and I've just updated RF to 3.2.0 SR1. I have the same situation - no logs from listener are printed out, breakpoints were missed, but the GUI control says that the file was successfully uploaded.

                              And I can't update to RF 3.2.1 SNAPSHOT because of an exception which I've described in a standalone topic.

                              What can I do wrong? I do everything by demo or dev guide recommendations. :(

                              Here is my code:

                              <h:panelGrid columns="2" columnClasses="fileupload_top,fileupload_top">
                               <rich:fileUpload fileUploadListener="#{fuMan.listener}"
                               id="upload"
                               onupload="Event.stop(event||window.event);"
                               uploadData="#{fuMan.fileList}"
                               maxFilesQuantity="#{fuMan.maxFiles}"
                               immediateUpload="#{fuMan.autoUpload}"
                               autoclear="#{fuMan.autoClear}"
                               acceptedTypes="#{fuMan.fileTypes}"
                               listHeight="#{fuMan.height}" listWidth="#{fuMan.width}">
                              
                               <!-- <a4j:support event="onuploadcomplete" reRender="info" /> -->
                              
                               <f:facet name="label">
                               <h:outputText value="{_KB}KB from {KB}KB uploaded --- {mm}:{ss}" />
                               </f:facet>
                              
                               </rich:fileUpload>
                              
                               <h:panelGroup>
                               <a4j:outputPanel id="info">
                               <h:outputText value="#{fuMan.fileListSize}"/>
                              
                               <rich:panel bodyClass="fileupload_info">
                               <f:facet name="header">
                               <h:outputText value="Uploaded Files Info" />
                               </f:facet>
                               <h:outputText value="No files currently uploaded"
                               rendered="#{fuMan.fileListSize==0}" />
                              
                               <rich:dataGrid columns="1" value="#{fuMan.fileList}"
                               var="file" rowKeyVar="row">
                               <rich:panel>
                               <h:panelGrid columns="2">
                               <a4j:mediaOutput element="img" mimeType="image/jpeg"
                               createContent="#{fuMan.paint}" value="#{row}"
                               style="width:100px; height:100px;" />
                               <h:panelGrid columns="2">
                               <h:outputText value="File Name:" />
                               <h:outputText value="#{file.name}" />
                               <h:outputText value="File Length(bytes):" />
                               <h:outputText value="#{file.length}" />
                               </h:panelGrid>
                               </h:panelGrid>
                               </rich:panel>
                               </rich:dataGrid>
                              
                               </rich:panel>
                               <rich:spacer height="3"/>
                               <br />
                               <a4j:commandButton action="#{fuMan.clearUploadData}"
                               reRender="info, upload" value="Clear Uploaded Data"
                               rendered="#{fuMan.fileListSize>0}" />
                              
                              
                               </a4j:outputPanel>
                               </h:panelGroup>
                              
                              </h:panelGrid>


                              and my listener is:
                              public void listener(UploadEvent event) throws Exception
                              {
                               UploadItem item = event.getUploadItem();
                               System.out.println("$$$$$$$$$--File : '" + item.getFileName() + "' was uploaded");
                               if(item.isFile())
                               {
                               File file = item.getFile();
                               System.out.println("Absolute Path : '" + file.getAbsolutePath() + "'!");
                               // storing file
                              
                              
                               //deleting file
                               file.delete();
                               }
                               else
                               {
                               ByteArrayOutputStream b = new ByteArrayOutputStream();
                               b.write(item.getData());
                               System.out.println(b.toString());
                               }
                              }



                              I'll appreciate any help, thanks in advance!

                              • 12. Re: file upload problems
                                Andrei Markavtsov Apprentice

                                What JSF version do you use?

                                Give me please link to topic with your problem described.

                                Thank you.

                                • 13. Re: file upload problems
                                  Yurii Kartsev Apprentice

                                  I use JSF 1.2 and link for 3.2.1 exception is here: http://www.jboss.com/index.html?module=bb&op=viewtopic&t=133869

                                  Thanks for the fast answer, waiting for any help...

                                  • 14. Re: file upload problems
                                    Andrei Markavtsov Apprentice

                                    Provide me please by full JSF version. 1.2_???

                                    Thank you.

                                    Some problem was detected with JSF 1.2_04.

                                    1 2 Previous Next