0

I am trying to upload a file in GWT , i am stuck in this for 2 days now .

Now i am trying some tutorial , so here's my code

My below code never goes to the server side ,any idea ..

or if some one can provide me with some working code ..

This is the code at my client side

   public class UploadDb extends Composite{

      private FlowPanel panelImages = new FlowPanel();


      public UploadDb() {
       initWidget(panelImages);

        // Create a new multiuploader and attach it to the document
        MultiUploader defaultUploader = new MultiUploader(FileInputType.LABEL);
        panelImages.add(defaultUploader);
        defaultUploader.setEnabled(true);

        // Add a finish handler which will load the image once the upload finishes
        defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);
      }

      // Load the image in the document and in the case of success attach it to the viewer
      private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
        public void onFinish(IUploader uploader) {
          if (uploader.getStatus() == Status.SUCCESS) {

            new PreloadedImage(uploader.fileUrl(), showImage);
        System.out.println("Server message " + uploader.fileUrl());
          }
        }
      };

      // Attach an image to the pictures viewer
      private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
        public void onLoad(PreloadedImage image) {
          image.setWidth("75px");
          panelImages.add(image);
        }
      };


    }

at my server side

               public class DashBoardUploadServlet  extends UploadAction {

      private static final long serialVersionUID = 1L;

      Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
      /**
       * Maintain a list with received files and their content types. 
       */
      Hashtable<String, File> receivedFiles = new Hashtable<String, File>();

      /**
       * Override executeAction to save the received files in a custom place
       * and delete this items from session.  
       */
      @Override
      public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
        String response = "";
        int cont = 0;
        for (FileItem item : sessionFiles) {
          if (false == item.isFormField()) {
            cont ++;
            try {
              /// Create a new file based on the remote file name in the client
              // String saveName = item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+", "_");
              // File file =new File("/tmp/" + saveName);

              /// Create a temporary file placed in /tmp (only works in unix)
              // File file = File.createTempFile("upload-", ".bin", new File("/tmp"));

              /// Create a temporary file placed in the default system temp folder
              File file = File.createTempFile("upload-", ".bin");
              item.write(file);

              /// Save a list with the received files
              receivedFiles.put(item.getFieldName(), file);
              receivedContentTypes.put(item.getFieldName(), item.getContentType());

              /// Send a customized message to the client.
              response += "File saved as " + file.getAbsolutePath();

            } catch (Exception e) {
              throw new UploadActionException(e.getMessage());
            }
          }}
      /// Remove files from session because we have a copy of them
        removeSessionFileItems(request);

        /// Send your customized message to the client.
        return response;
      }

web.xml

                <servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.dashboard.server.DashBoardUploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
   <servlet-name>UploadServlet</servlet-name>
   <url-pattern>/Upload</url-pattern>
   </servlet-mapping>
junaidp
  • 10,801
  • 29
  • 89
  • 137
  • Use FileUpload and add it to the formPanel. form.setEncoding(FormPanel.ENCODING_MULTIPART); form.setMethod(FormPanel.METHOD_POST); – swamy Feb 26 '13 at 12:07

4 Answers4

1

The servlet-mapping in your web.xml is incorrect. You have to change it to this:

<servlet-mapping>
  <servlet-name>UploadServlet</servlet-name>
  <url-pattern>*.gupld</url-pattern>
</servlet-mapping>

If you inspect the browser-server dialog with firebug or chrome dev-tools you should see 404 HTTP errors

Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27
  • 1
    Yes that is very true, by default gwtupload sets the servlet path to `servlet.gupld`, so the pattern `*.gupld` is the best option as they say in the [documentation](http://code.google.com/p/gwtupload/wiki/GwtUpload_GettingStarted). The developer can overwrite this path, in this case @junaidp should call `defaultUploader.setServletPath("/Upload")` if he doesnt want to change the `web.xml` as @Manolo suggests. –  Feb 27 '13 at 17:16
0

Try this code in client.

  public void onModuleLoad() {

    final FormPanel form = new FormPanel();
    form.setAction("/myFormHandler");

    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);
    VerticalPanel panel = new VerticalPanel();
    form.setWidget(panel);

    FileUpload upload = new FileUpload();
    upload.setName("uploadFormElement");
    panel.add(upload);

    // Add a 'submit' button.
    panel.add(new Button("Submit", new ClickListener() {
      public void onClick(Widget sender) {
        form.submit();
      }
    }));




    RootPanel.get().add(form);
  }

Follow this link file upload

Community
  • 1
  • 1
Parvathy
  • 2,275
  • 3
  • 24
  • 39
  • where will this file go after uploading , i want to do some working on this file , from where i can retrive this uploaded file – junaidp Feb 27 '13 at 05:54
  • you wil get this only in server check that link – Parvathy Feb 27 '13 at 05:58
  • but in development mode in GWT , is there a way i can get it ? – junaidp Feb 27 '13 at 07:52
  • using fileupload widget there is no way try using jsni. may be you will get – Parvathy Feb 27 '13 at 08:27
  • Precisely this is the goal of using gwtupload, apart from preparing the client side to correctly setup the form, hack the browser button to use any widget, send the file, deal with progress, it provides valid servlets for easily handling the file, even in GAE server and dev-mode, sending back server info to retrieve the file or preview images. – Manolo Carrasco Moñino Feb 27 '13 at 08:31
0

simply add your host path

String page=GWT.getModuleBaseURL()+"upload"; and your

servlet calling path defaultUploader.setServletPath(page); it perfectly work .. if you also want to upload file on local folder than add you drive path with foldername on servlet ..

0

You din't add your servlet path, add multiUploader.setServletPath("your_servlet_path"); also, submit the upload by adding multiuploader.setAutoSubmit(true)