Wt: Wt::WFileUpload Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Protected Member Functions
Wt::WFileUpload Class Reference

A widget that allows a file to be uploaded. More...

`#include <Wt/WFileUpload>`

Inheritance diagram for Wt::WFileUpload:
[legend]

List of all members.

## Public Member Functions
| WFileUpload (WContainerWidget *parent=0)
| Creates a file upload widget.
void | setMultiple (bool multiple)
| Sets whether the file upload accepts multiple files.
bool | multiple () const
| Returns whether multiple files can be uploaded.
void | setFileTextSize (int chars)
| Sets the size of the file input.
int | fileTextSize () const
| Returns the size of the file input.
std::string | spoolFileName () const
| Returns the spooled location of the uploaded file.
WString | clientFileName () const
| Returns the client filename.
WString | contentDescription () const
| Returns the client content description.
void | stealSpooledFile ()
| Steals the spooled file.
bool | empty () const
| Returns whether one or more files have been uploaded.
bool | emptyFileName () const
| Checks if no filename was given and thus no file uploaded. (Deprecated)
const std::vector < Http::UploadedFile > & | uploadedFiles () const
| Returns the uploaded files.
bool | canUpload () const
| Returns whether upload() will start a new file upload.
bool | isUploaded () const
| Returns whether the upload() slot will not start a new upload. (Deprecated)
EventSignal & | uploaded ()
| Signal emitted when a new file was uploaded.
Signal< ::int64_t > & | fileTooLarge ()
| Signal emitted when the user tried to upload a too large file.
EventSignal & | changed ()
| Signal emitted when the user selected a new file.
void | upload ()
| Starts the file upload.
void | setProgressBar (WProgressBar *progressBar)
| Sets a progress bar to indicate upload progress.
WProgressBar * | progressBar () const
| Returns the progress bar.
Signal< ::uint64_t,::uint64_t > & | dataReceived ()
| Signal emitted while a file is being uploaded.
virtual void | enableAjax ()
| Progresses to an Ajax-enabled widget.
## Protected Member Functions
virtual void | propagateSetEnabled (bool enabled)
| Propagates that a widget was enabled or disabled through children.

## Detailed Description

A widget that allows a file to be uploaded.

This widget is displayed as a box in which a filename can be entered and a browse button.

Depending on availability of JavaScript, the behaviour of the widget is different, but the API is designed in a way which facilitates a portable use.

When JavaScript is available, the file will not be uploaded until upload() is called. This will start an asynchronous upload (and thus return immediately).While the file is being uploaded, the dataReceived() signal is emitted when data is being received and if the connector library provides support (see also WResource::setUploadProgress() for a more detailed discussion). Although you can modify the GUI from this signal, you still need to have a mechanism in place to update the client interface (using a WTimer or using server-push). When the file has been uploaded, the uploaded() signal is emitted, or if the file was too large, the fileTooLarge() signal is emitted. You may configure a progress bar that is used to show the upload progress using setProgressBar(). When no JavaScript is available, the file will be uploaded with the next click event. Thus, upload() has no effect -- the file will already be uploaded, and the corresponding signals will already be emitted. To test if upload() will start an upload, you may check using the canUpload() call.

Thus, to properly use the widget, one needs to follow these rules:

- Be prepared to handle the uploaded() or fileTooLarge() signals also when upload() was not called.
- Check using canUpload() if upload() will schedule a new upload. if (!canUpload()) then upload() will not have any effect. if (canUpload()), upload() will start a new file upload, which completes succesfully using an uploaded() signal or a fileTooLarge() signals gets emitted.

The WFileUpload widget must be hidden or deleted when a file is received. In addition it is wise to prevent the user from uploading the file twice as in the example below.

The uploaded file is automatically spooled to a local temporary file which will be deleted together with the WFileUpload widget, unless stealSpooledFile() is called.

Usage example:

     Wt::WFileUpload *upload = new Wt::WFileUpload(this);
     upload->setFileTextSize(40);

     // Provide a button
     Wt::WPushButton *uploadButton = new Wt::WPushButton("Send", this);

     // Upload when the button is clicked.
     uploadButton->clicked().connect(upload, &Wt::WFileUpload::upload);
     uploadButton->clicked().connect(uploadButton, &Wt::WPushButton::disable);

     // Upload automatically when the user entered a file.
     upload->changed().connect(upload, &WFileUpload::upload);
     upload->changed().connect(uploadButton, &Wt::WPushButton::disable);

     // React to a succesfull upload.
     upload->uploaded().connect(this, &MyWidget::fileUploaded);

     // React to a fileupload problem.
     upload->fileTooLarge().connect(this, &MyWidget::fileTooLarge);

WFileUpload is an inline widget.

### CSS

The file upload itself corresponds to a `<input type="file">` tag, but may be wrapped in a `<form>` tag for an Ajax session to implement the asynchronous upload action. This widget does not provide styling, and styling through CSS is not well supported across browsers.

## Member Function Documentation

bool Wt::WFileUpload::canUpload | ( |  | ) | const

Returns whether upload() will start a new file upload.

A call to upload() will only start a new file upload if there is no JavaScript support. Otherwise, the most recent file will already be uploaded.

Note:
  This method was renamed, and its result inverted, from the now deprecated method isUploaded()

EventSignal & Wt::WFileUpload::changed | ( |  | ) |

Signal emitted when the user selected a new file.

One could react on the user selecting a (new) file, by uploading the file immediately.

Caveat: this signal is not emitted with konqueror and possibly other browsers. Thus, in the above scenario you should still provide an alternative way to call the upload() method.

WString Wt::WFileUpload::clientFileName | ( |  | ) | const

Returns the client filename.

When multiple files were uploaded, this returns the information from the first file.

WString Wt::WFileUpload::contentDescription | ( |  | ) | const

Returns the client content description.

When multiple files were uploaded, this returns the information from the first file.

Signal< ::uint64_t, ::uint64_t >& Wt::WFileUpload::dataReceived | ( |  | ) |

Signal emitted while a file is being uploaded.

When supported by the connector library, you can track the progress of the file upload by listening to this signal.

bool Wt::WFileUpload::emptyFileName | ( |  | ) | const

Checks if no filename was given and thus no file uploaded. (Deprecated)

Return whether a non-empty filename was given.

Deprecated:
  This method was renamed to empty()

void Wt::WFileUpload::enableAjax | ( |  | ) | `[virtual]`

Progresses to an Ajax-enabled widget.

This method is called when the progressive bootstrap method is used, and support for AJAX has been detected. The default behavior will upgrade the widget's event handling to use AJAX instead of full page reloads, and propagate the call to its children.

You may want to reimplement this method if you want to make changes to widget when AJAX is enabled. You should always call the base implementation.

See also:
  WApplication::enableAjax()

Reimplemented from Wt::WWebWidget.

Signal< ::int64_t >& Wt::WFileUpload::fileTooLarge | ( |  | ) |

Signal emitted when the user tried to upload a too large file.

The parameter is the (approximate) size of the file the user tried to upload.

The maximum file size is determined by the maximum request size, which may be configured in the configuration file (<max-request-size>).

See also:
  uploaded()
  WApplication::requestTooLarge()

bool Wt::WFileUpload::isUploaded | ( |  | ) | const

Returns whether the upload() slot will not start a new upload. (Deprecated)

Deprecated:
  use canUpload() instead -- canUpload() == !isUploaded(). the name was confusing.

bool Wt::WFileUpload::multiple | ( |  | ) | const

Returns whether multiple files can be uploaded.

See also:
  setMultiple()

WProgressBar* Wt::WFileUpload::progressBar | ( |  | ) | const

Returns the progress bar.

See also:
  setProgressBar()

void Wt::WFileUpload::propagateSetEnabled | ( | bool | enabled | ) | `[protected, virtual]`

Propagates that a widget was enabled or disabled through children.

When enabling or disabling a widget, you usually also want to disable contained children. This method is called by setDisabled() to propagate its state to all children.

You may want to reimplement this method if they wish to render differently when a widget is disabled. The default implementation will propagate the signal to all children.

Reimplemented from Wt::WWebWidget.

void Wt::WFileUpload::setMultiple | ( | bool | multiple | ) |

Sets whether the file upload accepts multiple files.

In browsers which support the "multiple" attribute for the file upload (to be part of HTML5) control, this will allow the user to select multiple files at once.

All uploaded files are available from uploadedFiles(). The single-file API will return only information on the first uploaded file.

The default value is `false`.

void Wt::WFileUpload::setProgressBar | ( | WProgressBar * | progressBar | ) |

Sets a progress bar to indicate upload progress.

When the file is being uploaded, upload progress is indicated using the provided progress bar. Both the progress bar range and values are configured when the upload starts.

If the provided progress bar already has a parent, then the file upload itself is hidden as soon as the upload starts. If the provided progress bar does not yet have a parent, then the bar becomes part of the file upload, and replaces the file prompt when the upload is started.

The default progress bar is 0 (no upload progress is indicated).

See also:
  dataReceived()

std::string Wt::WFileUpload::spoolFileName | ( |  | ) | const

Returns the spooled location of the uploaded file.

Returns the temporary filename in which the uploaded file was spooled. The file is guaranteed to exist as long as the WFileUpload widget is not deleted, or a new file is not uploaded.

When multiple files were uploaded, this returns the information from the first file.

See also:
  stealSpooledFile()
  uploaded

void Wt::WFileUpload::stealSpooledFile | ( |  | ) |

Steals the spooled file.

By stealing the file, the spooled file will no longer be deleted together with this widget, which means you need to take care of managing that.

When multiple files were uploaded, this returns the information from the first file.

void Wt::WFileUpload::upload | ( |  | ) |

Starts the file upload.

The uploaded() signal is emitted when a file is uploaded, or the fileTooLarge() signal is emitted when the file size exceeded the maximum request size.

See also:
  uploaded()
  canUpload()

EventSignal & Wt::WFileUpload::uploaded | ( |  | ) |

Signal emitted when a new file was uploaded.

This signal is emitted when file upload has been completed. It is good practice to hide or delete the WFileUpload widget when a file has been uploaded succesfully.

See also:
  upload()
  fileTooLarge()

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator