File upload

The WFileUpload widget allows you to upload a local file to the server.

Signals

To properly use this widget you need to handle the uploaded() or fileTooLarge() signals; even when upload() was not called.

An oversized file will result in a fileTooLarge() signal. The default threshold value (128) for this signal is defined in the general application settings file wt_config.xml by the parameter max-request-size (Kb). See the Library overview for more details.

Also check using canUpload() if upload() will schedule a new upload.

  • If (!canUpload()) then upload() will not have any effect.
  • If (canUpload()), then upload() will start a new file upload.
    If the upload completes successfully then an uploaded() signal will be emitted. Otherwise a fileTooLarge() signal will be emitted.

Example
source
  void FileUpload() {
    WContainerWidget container = new WContainerWidget();
    final WFileUpload fu = new WFileUpload((WContainerWidget) container);
    fu.setProgressBar(new WProgressBar());
    fu.setMargin(new WLength(10), EnumSet.of(Side.Right));
    final WPushButton uploadButton = new WPushButton("Send", (WContainerWidget) container);
    uploadButton.setMargin(new WLength(10), EnumSet.of(Side.Left, Side.Right));
    final WText out = new WText((WContainerWidget) container);
    uploadButton
        .clicked()
        .addListener(
            this,
            () -> {
              fu.upload();
              uploadButton.disable();
            });
    fu.changed()
        .addListener(
            this,
            () -> {
              fu.upload();
              uploadButton.disable();
              out.setText("File upload is changed.");
            });
    fu.uploaded()
        .addListener(
            this,
            () -> {
              out.setText("File upload is finished.");
            });
    fu.fileTooLarge()
        .addListener(
            this,
            () -> {
              out.setText("File is too large.");
            });
  }

File Drop

Try dropping files in the widget below. It accepts a maximum of 5 files. You can drop one or multiple files at a time.

Example
source
  void FileDrop() {
    WFileDropWidget dropWidgetPtr = new WFileDropWidget();
    final WFileDropWidget dropWidget = dropWidgetPtr;
    dropWidget
        .drop()
        .addListener(
            this,
            (List<WFileDropWidget.File> files) -> {
              final int maxFiles = 5;
              int prevNbFiles = dropWidget.getUploads().size() - files.size();
              for (int i = 0; i < files.size(); i++) {
                if (prevNbFiles + i >= maxFiles) {
                  dropWidget.cancelUpload(files.get(i));
                  continue;
                }
                WContainerWidget block = new WContainerWidget((WContainerWidget) dropWidget);
                block.setToolTip(files.get(i).getClientFileName());
                block.addStyleClass("upload-block spinner");
              }
              if (dropWidget.getUploads().size() >= maxFiles) {
                dropWidget.setAcceptDrops(false);
              }
            });
    dropWidget
        .uploaded()
        .addListener(
            this,
            (WFileDropWidget.File file) -> {
              List<WFileDropWidget.File> uploads = dropWidget.getUploads();
              int idx = 0;
              for (; idx != uploads.size(); ++idx) {
                if (uploads.get(idx) == file) {
                  break;
                }
              }
              dropWidget.getWidget(idx).removeStyleClass("spinner");
              dropWidget.getWidget(idx).addStyleClass("ready");
            });
    dropWidget
        .tooLarge()
        .addListener(
            this,
            (WFileDropWidget.File file, Long size) -> {
              List<WFileDropWidget.File> uploads = dropWidget.getUploads();
              int idx = 0;
              for (; idx != uploads.size(); ++idx) {
                if (uploads.get(idx) == file) {
                  break;
                }
              }
              dropWidget.getWidget(idx).removeStyleClass("spinner");
              dropWidget.getWidget(idx).addStyleClass("failed");
            });
    dropWidget
        .uploadFailed()
        .addListener(
            this,
            (WFileDropWidget.File file) -> {
              List<WFileDropWidget.File> uploads = dropWidget.getUploads();
              int idx = 0;
              for (; idx != uploads.size(); ++idx) {
                if (uploads.get(idx) == file) {
                  break;
                }
              }
              dropWidget.getWidget(idx).removeStyleClass("spinner");
              dropWidget.getWidget(idx).addStyleClass("failed");
            });
  }

Top