|
#include <Wt/WApplication>
|
|
#include <Wt/WContainerWidget>
|
|
#include <Wt/WPushButton>
|
|
#include <Wt/WText>
|
|
#include <Wt/WFileUpload>
|
|
#include <boost/filesystem.hpp>
|
|
|
|
using namespace Wt;
|
|
namespace bf = boost::filesystem; //create an alias
|
|
|
|
class FileUploadApplication : public WApplication
|
|
{
|
|
public:
|
|
FileUploadApplication(const WEnvironment& env);
|
|
|
|
private:
|
|
Wt::WText *status_;
|
|
Wt::WFileUpload *upload_;
|
|
|
|
void fileUploaded();
|
|
void fileTooLarge();
|
|
|
|
int count;
|
|
};
|
|
|
|
FileUploadApplication::FileUploadApplication(const WEnvironment& env)
|
|
: WApplication(env), count(0)
|
|
{
|
|
setTitle("Test File Upload");
|
|
|
|
Wt::WContainerWidget *wcont = new WContainerWidget(root());
|
|
|
|
status_ = new Wt::WText(wcont);
|
|
status_->setText("");
|
|
|
|
upload_ = new Wt::WFileUpload(wcont);
|
|
upload_->setFileTextSize(40);
|
|
|
|
// Provide a button
|
|
Wt::WPushButton *uploadButton = new Wt::WPushButton("Send", wcont);
|
|
|
|
// 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, &FileUploadApplication::fileUploaded);
|
|
|
|
// React to a fileupload problem.
|
|
upload_->fileTooLarge().connect(this, &FileUploadApplication::fileTooLarge);
|
|
}
|
|
|
|
void FileUploadApplication::fileUploaded()
|
|
{
|
|
count++;
|
|
|
|
std::cerr << "FileUploadApplication::fileUploaded() count[" << count << "]" << std::endl;
|
|
|
|
std::string wfn = upload_->spoolFileName();
|
|
status_->setText(wfn);
|
|
upload_->stealSpooledFile();
|
|
|
|
try
|
|
{
|
|
// ...process file
|
|
bf::remove(wfn);
|
|
status_->setText(WString("File [{1}] saved").arg(wfn));
|
|
}
|
|
catch(bf::filesystem_error e)
|
|
{
|
|
bf::remove(wfn);
|
|
status_->setText(e.what());
|
|
}
|
|
catch(std::exception e)
|
|
{
|
|
bf::remove(wfn);
|
|
status_->setText(e.what());
|
|
}
|
|
|
|
upload_->disable();
|
|
|
|
std::cerr << "FileUploadApplication::fileUploaded() end count[" << count << "]" << std::endl;
|
|
|
|
}
|
|
|
|
void FileUploadApplication::fileTooLarge()
|
|
{
|
|
std::cerr << "FileUploadApplication::fileTooLarge()" << std::endl;
|
|
|
|
status_->setText("File Too Large ");
|
|
}
|
|
|
|
WApplication *createApplication(const WEnvironment& env)
|
|
{
|
|
|
|
return new FileUploadApplication(env);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
|
|
return WRun(argc, argv, &createApplication);
|
|
}
|