Reputation: 1405
I am new to WT, i am trying the upload file example . The code works fine when i click the send button the file progress bar runs to 100% but i am not sure where it is uploaded ? can we define to upload in certain path..
class HelloApplication: public WApplication {
public:
HelloApplication(const WEnvironment& env);
private:
WPushButton *uploadButton;
Wt::WFileUpload *fu;
void greet();
};
HelloApplication::HelloApplication(const WEnvironment& env) :
WApplication(env) {
root()->addStyleClass("container");
setTitle("Hello world"); // application title
fu = new Wt::WFileUpload(root());
fu->setFileTextSize(50); // Set the maximum file size to 50 kB.
fu->setProgressBar(new Wt::WProgressBar());
fu->setMargin(10, Wt::Right);
// Provide a button to start uploading.
uploadButton = new Wt::WPushButton("Send", root());
uploadButton->setMargin(10, Wt::Left | Wt::Right);
// Upload when the button is clicked.
uploadButton->clicked().connect(this, &HelloApplication::greet);
}
void HelloApplication::greet() {
fu->upload();
uploadButton->disable();
}
WApplication *createApplication(const WEnvironment& env) {
return new HelloApplication(env);
}
int main(int argc, char **argv) {
return WRun(argc, argv, &createApplication);
}
Upvotes: 4
Views: 1582
Reputation: 1128
I realise this is an old post but I also had issues and the question wasn't quite answered (specifically the uploadedFiles function that is needed to read the contents of the file)
In your constructor (i.e. the HelloApplication::HelloApplication function) add this to react to the fileUploaded signal:
uploadButton->uploaded().connect(this, &HelloApplication::fileUploaded);
Then add a function like this to read the contents of the file:
void HelloApplication::fileUploaded() {
//The uploaded filename
std::string mFilename = fu->spoolFileName();
//The file contents
std::vector<Wt::Http::UploadedFile> mFileContents = fu->uploadedFiles();
//The file is temporarily stored in a file with location here
std::string mContents;
mContents=mFileContents.data()->spoolFileName();
//Do something with the contents here
//Either read in the file or copy it to use it
//return
return;
}
I hope this helps anyone else redirected here.
Upvotes: 3
Reputation: 3058
A WFileUpload will fire a signal (uploaded()) when the file is completed. Then look at spoolFileName() to get the filename of the file on your local disk. Listen on fileTooLarge() too, since it will inform you that the upload failed.
The manual of WFileUpload comes with a lot of information and a code example: http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WFileUpload.html
Upvotes: 4