1. #1
    Sencha User
    Join Date
    Jul 2012
    Posts
    27
    Vote Rating
    0
    dissectcode is on a distinguished road

      0  

    Default Help with file upload

    Help with file upload


    Hi - I have spent 8 days following tutorials and blogs and tried everything. I have this code below...but the command object in the onSubmit method is always null and I don't know why the file isn't being sent by Extjs... Please help

    I am using extjs 3 :

    Code:
    createFileUpload = function()
    {
        var URL = "filePreview.do";
            
            myuploadform= new Ext.FormPanel({
                    fileUpload: true,
                    width: 250,
                    autoHeight: true,
                    bodyStyle: 'padding: 10px 10px 10px 10px;',
                    labelWidth: 50,
                    items:[
                    {
                        xtype: 'fileuploadfield',
                        id: 'filedata',
                        emptyText: 'Select a document to upload...',
                        fieldLabel: 'File',
                        buttonText: 'Browse'
                    }],
                    buttons: [{
                        text: 'Preview',
                        handler: function(){
                   
                            if(myuploadform.getForm().isValid()){
                                form_action=1;
                                myuploadform.getForm().submit({
                                    url: URL,
                                    waitMsg: 'Uploading file...',
                                    headers: {'Content-type':'multipart/form-data'},
                                    success: function(form,action){
                                        alert('Success', 'Processed file on the server');
                                    },
                                    failure: function() { alert("this failed too."); }
                                });
                            }
                        }
                    }]
                })
                
              return myuploadform;
    }
    I have a large form of ~50 items and I am trying to add this Extjs file upload field, with a "preview" button to open the file for verification (requested by the customer). So far I am just trying to save the file I select; I have not implemented the GET part of the preview yet.

    I am using Java 6 with Spring 2.0.8.

    Bean #1 :

    Code:
    package beans;
    
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    
    
    import org.springframework.web.multipart.MultipartFile;
    
    
    public class FilePreviewBean
    {        
        private MultipartFile file;
        
        public void setFile( MultipartFile file )
        {
            this.file = file;
        }
        
        public MultipartFile getFile()
        {
            return file;
        }
    }

    The controller bean :
    Code:
    package controller;
    
    public class FilePreviewController extends SimpleFormController
    {
        
        public FilePreviewController()
        {
        }
        
        protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors ) throws ServletException, IOException 
        {
    // cast the bean
            FilePreviewBean fp = (FilePreviewBean)command;    
            MultipartFile file = fp.getFile();
            
    
    
            if( file == null )   // <------------- IT'S ALWAYS NULL ?!
            {
                  System.out.println("The file was null.");
                 return null;
            }
            else
            {
                    // print/return an error
                    return null;
             }
        }
        
        protected void initBinder( HttpServletRequest request, ServletRequestDataBinder binder ) throws ServletException
            {
    //binder.registerCustomEditor( byte[].class, new ByteArrayMultipartFileEditor() );
            binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
        }
    
    
    }
    And the bean definitions :

    Code:
        <bean id="multiPartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
            
        <bean id="filePreviewController" class="controller.FilePreviewController"> 
            <property name="commandClass" value="cbeans.FilePreviewBean"/>
            <property name="formView" value="fileuploadform"/>
        </bean>
    I can see that the bean is called from my preview button. The setFile() method should apparently be called automatically by Spring MVC but when I call getFile() it always returns null.

    I was trying to follow this : http://static.springsource.org/sprin...#mvc-multipart

    and it makes a lot of sense; but I have no idea what to do to make the file not equal null!? Please help

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,656
    Vote Rating
    435
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    So is the request being sent to upload?
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Jul 2012
    Posts
    27
    Vote Rating
    0
    dissectcode is on a distinguished road

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    So is the request being sent to upload?
    by the time it go to onSubmit() the "Object command" is null. I don't know where the disconnect is.