Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    UploadItem: How to get event when file is successfully Uploaded?

    Hello,
    I'm new to SmartGWT. I'm using SmartGWT(LGPL) 2.2 with FF 3.6.
    I've done the file upload module in my GWT-Ext project successfully. In that I was using a FormPanel & a FileUpload control. After the file is uploaded I handled the event like this:
    Code:
    formPanel.addFormListener(new FormListenerAdapter(){
    			@Override
    			public void onActionComplete(Form form, int httpStatus,
    					String responseText) {
    				MessageBox.alert("Succesful");
    			}
    			@Override
    			public void onActionFailed(Form form, int httpStatus,
    					String responseText) {
    				MessageBox.alert("Failed");
    			}
    			
    		});
    I used Struts to send request to my Action Class & redirect it back to the listener like this:
    Code:
    <package name="example2" extends="struts-default">
    		<action name="FileUploadAction" class="com.nextenders.server.actions.FileUploadAction">	
    			<result name="success" type="redirect">success</result>
    			<result name="error">Fail</result>
    			<result name="none">None</result>
    			<result name="Empty">Empty</result>
    			<result name="SIZE">SIZE</result>
    		</action>
    	</package>
    I'm using DynamicForm and UploadItem of SmartGWT.
    can anyone tell me if there is a similar functionality like formPanel.addFormListener() in DynamicForm of SmartGWT?

    #2
    How you solved it?

    Comment


      #3
      you can easily use an gwt "FormPanel" instead of an smartgwt "DynamicForm". The form panel has an listener.


      Pop Up Window with upload item (place it in ClientPackage):
      Code:
      package com.uds.webadmin.client;
      
      import java.util.Date;
      
      import com.google.gwt.core.client.GWT;
      import com.google.gwt.i18n.client.DateTimeFormat;
      import com.google.gwt.user.client.DOM;
      import com.google.gwt.user.client.Element;
      import com.google.gwt.user.client.ui.FileUpload;
      import com.google.gwt.user.client.ui.FormPanel;
      import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
      import com.google.gwt.user.client.ui.VerticalPanel;
      import com.smartgwt.client.util.SC;
      import com.smartgwt.client.widgets.Window;
      import com.smartgwt.client.widgets.events.ClickEvent;
      import com.smartgwt.client.widgets.events.ClickHandler;
      import com.smartgwt.client.widgets.form.DynamicForm;
      import com.smartgwt.client.widgets.form.fields.SpacerItem;
      import com.smartgwt.client.widgets.form.fields.StaticTextItem;
      import com.smartgwt.client.widgets.layout.VLayout;
      import com.smartgwt.client.widgets.toolbar.ToolStrip;
      import com.smartgwt.client.widgets.toolbar.ToolStripButton;
      
      public class GPopUpUpload
      {
          private FormPanel form;
          private VerticalPanel panel;
          private FileUpload uploader;
          private GEditEmailForm emailForm;
          private DynamicForm editorForm;
          private Window window;
      
          public GPopUpUpload( GEditEmailForm emailForm )
          {
              this.emailForm = emailForm;
          }
      
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          /**
           * Creates a pop up window
           */
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          public void initGUI()
          {
              window = new Window();
              window.setSize( "475px", "135px" );
              window.setTitle( "Import" );
              window.centerInPage();
              window.addItem( getEditForm() );
              window.draw();
          }
      
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          /**
           * Creates and returns an editor form
           */
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          private VLayout getEditForm()
          {
              VLayout layout = new VLayout();
      
              {
                  DynamicForm spacerForm = new DynamicForm();
                  spacerForm.setHeight( "35%" );
                  layout.addMember( spacerForm );
      
                  editorForm = new DynamicForm();
                  editorForm.setWidth( "100%" );
                  editorForm.setHeight( "10px" );
                  editorForm.setNumCols( 2 );
                  editorForm.setColWidths( 5, "*" );
                  editorForm.setMargin( 3 );
      
                  StaticTextItem txtText = new StaticTextItem( "Text", "" );
                  txtText.setValue( "Please choose your import file: " );
                  editorForm.setItems( txtText );
      
                  layout.addMember( spacerForm );
                  layout.addMember( editorForm );
                  layout.addMember( getUploadForm() );
              }
              {
                  ToolStrip toolbar = new ToolStrip();
                  toolbar.setWidth100();
                  toolbar.setMembersMargin( 5 );
                  toolbar.setReverseOrder( true );
                  {
      
                      ToolStripButton buttonOK = new ToolStripButton( "Apply" );
                      buttonOK.setIcon( "icons/32x32/apply.png" );
                      buttonOK.addClickHandler( new ClickHandler()
                      {
                          public void onClick( ClickEvent event )
                          {
                              uploadFile();
                              closeWindow();
                          }
                      } );
      
                      toolbar.addButton( buttonOK );
      
                      ToolStripButton buttonCancel = new ToolStripButton( "Cancel" );
                      buttonCancel.setIcon( "icons/32x32/cancel.png" );
                      buttonCancel.addClickHandler( new ClickHandler()
                      {
                          public void onClick( ClickEvent event )
                          {
                              closeWindow();
                          }
                      } );
                      toolbar.addButton( buttonCancel );
      
                      layout.addMember( toolbar );
                  }
              }
              return layout;
          } // End of method
      
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          /**
           * Creates and returns an upload panel
           */
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          private FormPanel getUploadForm()
          {
              // Create a FormPanel and point it at a service.
              form = new FormPanel();
              form.setHeight( "20px" );
              form.setAction( "/myFormHandler" );
      
              SpacerItem spacer = new SpacerItem();
              spacer.setWidth( "10px" );
      
              // Because we're going to add a FileUpload widget, we'll need to set the
              // form to use the POST method, and multipart MIME encoding.
              form.setEncoding( FormPanel.ENCODING_MULTIPART );
              form.setMethod( FormPanel.METHOD_POST );
      
              // Create a panel to hold all of the form widgets.
              panel = new VerticalPanel();
              form.setWidget( panel );
      
              // Create a FileUpload widget.
              uploader = new FileUpload();
      
              // determines uploader width... make Firefox 1.5.0.7 or higher happy
              Element ee = uploader.getElement();
              DOM.setAttribute( ee, "size", "50" );
      
              uploader.setName( "Uploader" );
              panel.add( uploader );
              panel.setSpacing( 12 );
      
              form.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler()
              {
                  public void onSubmitComplete( SubmitCompleteEvent event )
                  {
                      // When the form submission is successfully completed, this event is
                      // fired. Assuming the service returned a response of type text/html,
                      // we can get the result text here (see the FormPanel documentation for
                      // further explanation).
                      SC.say( event.getResults() );
                  }
              } );
      
              return form;
          }
      
          private void uploadFile()
          {
              if ( uploader.getFilename().equals( "" ) == false )
              {
                  StringBuilder url = new StringBuilder();
                  url.append( "temp?file=" + getFileName() );
                  form.setAction( GWT.getHostPageBaseURL() + ( url.toString() ) );
                  form.submit();
                  closeWindow();
              }
              else
              {
                  SC.say( "Please choose a file before clicking the button 'Apply'" );
              }
      
          }
      
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          /**
           * Creates and returns a file name
           */
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          private String getFileName()
          {
              String fileName = null;
      
              Date date = new Date();
              DateTimeFormat fmt = DateTimeFormat.getFormat( "yyyy_MM_dd_H_m_s_" );
              String formatedDate = fmt.format( date );
      
              fileName = formatedDate + GWebAdmin.loginName + getMimeType( uploader.getFilename() );
      
              return fileName;
          }
      
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          /**
           * Returns the mimetype of a file
           */
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          private String getMimeType( String filePath )
          {
      
              int i = filePath.lastIndexOf( "." );
              String fileType = null;
      
              if ( i > -1 && i < filePath.length() - 1 )
              {
                  fileType = filePath.substring( i );
              }
      
              return fileType;
      
          } // Ende der Methode
      
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          /**
           * Closes the opened window
           */
          // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          private void closeWindow()
          {
              window.destroy();
          } // End of class
      
      }

      Servlet for uploading the file (place it in ServerPackage)::
      Code:
      package com.uds.webadmin.server;
      
      import java.io.File;
      import java.io.IOException;
      import java.util.List;
      
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import org.apache.commons.fileupload.FileItem;
      import org.apache.commons.fileupload.FileItemFactory;
      import org.apache.commons.fileupload.disk.DiskFileItemFactory;
      import org.apache.commons.fileupload.servlet.ServletFileUpload;
      
      
      public class CServletTempFile
          extends HttpServlet
      {
      
          @Override
          protected void doPost( HttpServletRequest req, HttpServletResponse resp )
              throws ServletException, IOException
          {
      
              String UPLOAD_DIRECTORY = getServletContext().getRealPath( "/" ) + "temp";
      
              // process only multipart requests
              if ( ServletFileUpload.isMultipartContent( req ) )
              {
      
                  // Create a factory for disk-based file items
                  FileItemFactory factory = new DiskFileItemFactory();
      
                  // Create a new file upload handler
                  ServletFileUpload upload = new ServletFileUpload( factory );
      
                  // Parse the request
                  try
                  {
                      List<FileItem> items = upload.parseRequest( req );
                      for ( FileItem item : items )
                      {
                          // process only file upload - discard other form item types
                          if ( item.isFormField() )
                              continue;
      
                          // Reads the fileName from URL
                          String fileName = req.getParameter( "file" );
      
                          File uploadedFile = new File( UPLOAD_DIRECTORY, fileName );
                          if ( uploadedFile.createNewFile() )
                          {
                              item.write( uploadedFile );
                              resp.setStatus( HttpServletResponse.SC_CREATED );
                              resp.getWriter().print( "The file was created successfully." );
                              resp.flushBuffer();
                          }
                          else
                              throw new IOException( "The file already exists in repository." );
                      }
                  }
                  catch ( Exception e )
                  {
                      resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                      "An error occurred while creating the file : " + e.getMessage() );
                  }
              }
              else
              {
                  resp.sendError( HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
                                  "Request contents type is not supported by the servlet." );
              }
          }
      
      }

      Add to your web.xml the sevlet
      Code:
      <!-- Temp FileUpload to application server -->
      	<servlet>
           	<servlet-name>WriteTempFile</servlet-name>
           	<servlet-class>com.uds.webadmin.server.CServletTempFile</servlet-class>
      	</servlet>
      	<servlet-mapping>
           	<servlet-name>WriteTempFile</servlet-name>
           	<url-pattern>/temp</url-pattern>
      	</servlet-mapping>

      important create ine "war" directory a new folder called "temp"
      Last edited by andyx1975; 23 Feb 2012, 08:52.

      Comment

      Working...
      X