A widget that allows a file to be uploaded. More...
#include <Wt/WFileUpload>
Public Member Functions | |
WFileUpload (WContainerWidget *parent=0) | |
Creates a file upload widget. | |
void | setFileTextSize (int chars) |
Sets the size of the file input. | |
int | fileTextSize () const |
Returns the size of the file input. | |
const std::string & | spoolFileName () const |
Returns the spooled location of the uploaded file. | |
const WString & | clientFileName () const |
Returns the client filename. | |
const WString & | contentDescription () const |
Returns the client content description. | |
void | stealSpooledFile () |
Steals the spooled file. | |
bool | emptyFileName () const |
Checks if no filename was given and thus no file uploaded. | |
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< int > & | 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. | |
virtual void | enableAjax () |
Progresses to an Ajax-enabled widget. |
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). When the file has been uploaded, the uploaded() signal is emitted, or if the file was too large, the fileTooLarge() signal is emitted.
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:
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(SLOT(upload, Wt::WFileUpload::upload)); uploadButton->clicked().connect(SLOT(uploadButton, Wt::WPushButton::disable)); // Upload automatically when the user entered a file. upload->changed().connect(SLOT(upload, WFileUpload::upload)); upload->changed().connect(SLOT(uploadButton, Wt::WPushButton::disable)); // React to a succesfull upload. upload->uploaded().connect(SLOT(this, MyWidget::fileUploaded)); // React to a fileupload problem. upload->fileTooLarge().connect(SLOT(this, MyWidget::fileTooLarge));
WFileUpload is an inline widget.
The file upload itself corresponds to a <input type="file">
tag, but may be wrapped in a <form>
tag. This widget does not provide styling, and styling through CSS is not well supported across browsers.
bool Wt::WFileUpload::canUpload | ( | ) | const [inline] |
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.
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.
bool Wt::WFileUpload::emptyFileName | ( | ) | const [inline] |
Checks if no filename was given and thus no file uploaded.
Return whether a non-empty filename was given.
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.
Reimplemented from Wt::WWebWidget.
Signal<int>& Wt::WFileUpload::fileTooLarge | ( | ) | [inline] |
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>).
bool Wt::WFileUpload::isUploaded | ( | ) | const [inline] |
Returns whether the upload() slot will not start a new upload. (Deprecated)
const std::string& Wt::WFileUpload::spoolFileName | ( | ) | const [inline] |
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.
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.
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.
EventSignal & Wt::WFileUpload::uploaded | ( | ) |
Signal emitted when a new file was uploaded.
This signal is emitted when a new file has been received. It is good practice to hide or delete the WFileUpload widget when a file has been uploaded succesfully.