RAP/Incubator/File Upload

From Eclipsepedia

< RAP
Jump to: navigation, search

The "File Upload" incubator project aims to provide an easy and complete solution for uploading files from a users file system to the RAP server.

As of version 1.4, a FileUpload widget is part of RWT. (It replaced the old Upload widget from the sandbox.) It looks like a button and basically wraps the HTML file selection <input> element. When it’s pressed, a native file dialog opens up that lets users select a file from their local file system. On file selection, a SelectionEvent will be fired. You can then programmatically upload the selected file to an http server using FileUpload.submit( URL ).

FileUploadHandler

In order to receive and store the uploaded files on the server, you also need a server-side component. This project provides the so called FileUploadHandler, which uses the Apache fileupload component internally. It’s included in the bundle org.eclipse.rap.rwt.supplemental.fileupload. This handler accepts file uploads to a certain URL (FileUploadHandler.getUploadUrl()) and delegates the data to a FileUploadReceiver. You can either use the provided DiskFileUploadReceiver or create your own receiver to do whatever you like with the uploaded data: put it into a database, or simple analyze the data and discard it.

FileDialog

This project also provides an implementation of the SWT FileDialog, which encapsulated the entire upload process. To make it easy to use, its provide as an update site for the Incubator. To use the FileDialog in your application, all you have to do is to

  1. include the bundles from the RAP Incubator repository in your RAP target platform, and
  2. add a bundle dependency to org.eclipse.rap.rwt.supplemental.filedialog to your project. (You have to use Require-Bundle here because this bundle contributes a class to the org.eclipse.swt.widgets package, effectively creating a split-package).

That’s all. Now you can use the FileDialog just like in SWT:

  FileDialog fileDialog = new FileDialog( shell, SWT.TITLE | SWT.MULTI );
  fileDialog.setText( "Upload Files" );
  fileDialog.setAutoUpload( true );
  fileDialog.open();
  String[] fileNames = fileDialog.getFileNames();

After uploading, the dialog closes and the variable fileNames contains the absolute file names of the uploaded files on the server’s file system. There’s an auto-upload feature – with autoUpload on, the upload starts immediately after file selection. A user can still press Cancel to prevent the application from using the uploaded files.