Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

RAP/Incubator/File Upload

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 include the bundles from the RAP Incubator repository in your RAP target platform, and add a bundle dependency to org.eclipse.rap.rwt.supplemental.filedialog to your project. (>ou 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 ); // This API will change, see below!
 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 that is really nice (I think it should be the default) – 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.

Note: Please note that this stuff is in the incubator and not part of the 1.4 release. The API and implementation may (and will) have to change and mature over time. However, if you use the latest version from 1.4/incubator site, you’ll always get a file dialog that will work with RAP 1.4. The server-side upload receiver and the required Apache bundles are also included.

We hope you enjoy these new features. Please try them out, tell us what you think, open bugs for the new stuff, and help us improving them.

Kudos to our new RAP committers Austin Riddle and Cole Markham who created this great new feature!

Update: I mistakenly left out the “.rwt.” from the bundle namespace in the original post, the bundle names are fixed now in the text.

Update: The 1.4 incubator repository has been updated with a newer version of the file dialog that is compatible with RAP 1.4. This update fixes the problem with missing file names mentioned in the comments.

Back to the top