我是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);
}发布于 2013-04-24 16:56:21
当文件完成时,WFileUpload将发出一个信号(uploaded())。然后查看spoolFileName()以获取本地磁盘上文件的文件名。还要监听fileTooLarge(),因为它会通知您上传失败。
WFileUpload手册附带了大量信息和一个代码示例:http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WFileUpload.html
发布于 2015-02-02 11:00:05
我意识到这是一个老帖子,但我也有问题,问题没有得到很好的回答(特别是读取文件内容所需的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;
}我希望这能帮助其他人转到这里。
https://stackoverflow.com/questions/16054581
复制相似问题