10 Replies Latest reply on Apr 28, 2008 3:49 PM by egil

    Tomahawk fileupload - How?

    silverjam

      Hi there,

      Way too many problems with the RichFaces fileupload, so I'm trying with the Tomahawk fileupload instead. However I cannot get it to work... :-(

      Does anybody know if it's possible at all or?

      This is part of my web.xml:

       <filter>
       <display-name>RichFaces Filter</display-name>
       <filter-name>richfaces</filter-name>
       <filter-class>org.ajax4jsf.Filter</filter-class>
       </filter>
      
       <filter>
       <filter-name>tomahawk</filter-name>
       <filter-class>
       org.apache.myfaces.component.html.util.ExtensionsFilter
       </filter-class>
       <init-param>
       <param-name>uploadMaxFileSize</param-name>
       <param-value>10m</param-value>
       </init-param>
       <init-param>
       <param-name>uploadThresholdSize</param-name>
       <param-value>100k</param-value>
       </init-param>
       </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>
      
       <filter-mapping>
       <filter-name>tomahawk</filter-name>
       <servlet-name>Faces Servlet</servlet-name>
       </filter-mapping>
      


      I do <h:form enctype="multipart/form-data"> in my code, but somehow the file doesn't get uploaded. Is the file gobbled by a filter or am I missing something else?

      Please help,

      Morten :-)

        • 1. Re: Tomahawk fileupload - How?
          ilya_shaikovsky

          using tomahawk fileUpload - you should not use ajax submit to upload file. It will never work according to XMLHttpRequest security limitations.

          • 2. Re: Tomahawk fileupload - How?
            egil

            You can use Tomahawk file upload with richfaces, I've done it before. What you can't use it for is ajax upload however.

            However I have also got some of these random, won't-work-at-all uploads (with uploadedfile being null etc). For some reason it seems like it's behaviour is a little random.

            You should also make sure you have:

            <h:form enctype="multipart/form-data">


            with your form.

            Finally, when using Tomahawk (1.6 at least) with Richfaces, you need to patch it as it includes a different version of Prototype.js than Richfaces. To fix this, unzip Tomahawk.jar and Richfaces.jar, replace the Prototype.js in tomahawk with the one from richfaces. Pack it up again, rename to .jar and it should work without these pesky 'stack overflow messages' that tend to appear when mixing Prototype-versions.

            Hope this helps you a little bit at least :)

            • 3. Re: Tomahawk fileupload - How?
              silverjam

              Thank you for your fast replies! :)

              "egil" wrote:
              However I have also got some of these random, won't-work-at-all uploads (with uploadedfile being null etc). For some reason it seems like it's behaviour is a little random.

              You should also make sure you have:

              <h:form enctype="multipart/form-data">


              with your form.


              Hm.... I get null in uploadedFile, even with enctype set as mentioned. :(

              When I first tried the Tomahawk upload control, it was living besides the RichFaces upload control and it would receive uploaddata fine. Not sure why it's broken after I removed the RichFaces control....

              Pretty annoying if the behaviour is a bit random.

              I think I read somewhere that RichFaces doesn't send the data further down the filter-chain with certain enctypes. Does this have anything on it?

              Does anybody have a clue on this, or even a working example?

              Cheers,
              ~Morten :-)

              • 4. Re: Tomahawk fileupload - How?
                ilya_shaikovsky

                you remember that you can't submit t:fileUpload with a4j:* controls?

                In other words you shouldn't use ajax submit with input type="file".

                • 5. Re: Tomahawk fileupload - How?
                  silverjam

                   

                  "ilya_shaikovsky" wrote:
                  you remember that you can't submit t:fileUpload with a4j:* controls?

                  In other words you shouldn't use ajax submit with input type="file".


                  I'm submitting with a a4j:commandButton... That's pretty bad then?

                  Would it work with a regular <h:commandButton> with <a4j:support> tags inside?

                  • 6. Re: Tomahawk fileupload - How?
                    ilya_shaikovsky

                    As I told you earlier XMLHttpRequest object can't be used at all to file uploading! So a4j:* tags should not be used together with t:fileUpload.

                    Use rich file upload.

                    • 7. Re: Tomahawk fileupload - How?
                      silverjam

                       

                      "ilya_shaikovsky" wrote:
                      Use rich file upload.


                      I wish I could....

                      Problem is that the user should only be able to upload a single file. However, the maxFilesQuantity property defines how many times a user should be able to upload a file, not the maximum number of files in the upload list. (Which is totally stupid IMHO.)

                      If I set maxFilesQuantity="1", hitting the "Clear" button (or the link in the list) in the fileUpload doesn't reset the "Add" button. The only way to to this is to do a rerender of the fileUpload control. It is not possible to assign this behaviour to the "Clear" button (or link), so I'm stuck....

                      Any ideas?

                      ~Morten :-)

                      • 8. Re: Tomahawk fileupload - How?
                        egil

                        I have some working code back for tomahawk file upload at work, I can post it tomorrow morning :)

                        It is correct that you need to use <h:commandButton> and not <a4j:commandButton> for the submit by the way, and the enc-type must be set to what I said earlier.


                        If I set maxFilesQuantity="1", hitting the "Clear" button (or the link in the list) in the fileUpload doesn't reset the "Add" button. The only way to to this is to do a rerender of the fileUpload control. It is not possible to assign this behaviour to the "Clear" button (or link), so I'm stuck....


                        You can't do a onclear=someScript(); to reset your uploads in the backing bean then to enforce maximum one file. The someScript() should then be a <a4j:jsFunction> with a reRender covering the upload-field. Should work, although not the cleanest implementation. Just a thought though ... :)

                        • 9. How to make fileUpload Clear button work properly :-)
                          silverjam

                          Hehe.... Seems like I found a way out of my troubles. Just needed to dig one step deeper. Here's my findings which might help other people out there.

                          This gave me the idea: http://jira.jboss.org/jira/browse/RF-2961

                          The feature didn't make it on 3.2.0-SR1, so in order to get things to work, I found a nightly build of RichFaces 3.2.1 (all 3 jars).

                          Anyways: this is my code where hitting the "Clear" link will reset the control:

                          <a4j:form>
                          
                           <rich:fileUpload id="fileUpload"
                           fileUploadListener="#{MyBackingBean.fileUploadListener}"
                           immediateUpload="true"
                           maxFilesQuantity="1"
                           onclear="fileUploadClear()"
                           required="true"/>
                          
                           <a4j:jsFunction name="fileUploadClear" reRender="fileUpload"/>
                          
                           <a4j:commandButton value="Submit form" action="#{MyBackingBean.submitAction}"/>
                          
                          </a4j:form>
                          


                          Cheers,
                          ~Morten :-)

                          • 10. Re: How to make fileUpload Clear button work properly :-)
                            egil

                             

                            "silverjam" wrote:
                            Hehe.... Seems like I found a way out of my troubles. Just needed to dig one step deeper. Here's my findings which might help other people out there.

                            This gave me the idea: http://jira.jboss.org/jira/browse/RF-2961

                            The feature didn't make it on 3.2.0-SR1, so in order to get things to work, I found a nightly build of RichFaces 3.2.1 (all 3 jars).

                            Anyways: this is my code where hitting the "Clear" link will reset the control:

                            <a4j:form>
                            
                             <rich:fileUpload id="fileUpload"
                             fileUploadListener="#{MyBackingBean.fileUploadListener}"
                             immediateUpload="true"
                             maxFilesQuantity="1"
                             onclear="fileUploadClear()"
                             required="true"/>
                            
                             <a4j:jsFunction name="fileUploadClear" reRender="fileUpload"/>
                            
                             <a4j:commandButton value="Submit form" action="#{MyBackingBean.submitAction}"/>
                            
                            </a4j:form>
                            


                            Cheers,
                            ~Morten :-)


                            Same as my suggestion then, cheers ! :)