我是WT新手,我正在尝试上传文件示例。 当我单击发送按钮文件进度条运行到100%时,代码工作正常,但我不知道它在哪里上传?我们可以定义上传某个路径..
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);
}
答案 0 :(得分:4)
WFileUpload将在文件完成时触发信号(uploaded())。然后查看spoolFileName()以获取本地磁盘上文件的文件名。同时监听fileTooLarge(),因为它会通知您上传失败。
WFileUpload手册附带了大量信息和代码示例: http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WFileUpload.html
答案 1 :(得分:3)
我意识到这是一个老帖子,但我也有问题,问题没有得到很好的回答(特别是读取文件内容所需的 uploadedFiles 功能)
在你的构造函数(即HelloApplication :: HelloApplication函数)中添加它以响应fileUploaded信号:
uploadButton->uploaded().connect(this, &HelloApplication::fileUploaded);
然后添加这样的函数来读取文件的内容:
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;
}
我希望这可以帮助其他重定向的人。