4

我是 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);
}
4

2 回答 2

4

当文件完成时,WFileUpload 将触发一个信号 (uploaded())。然后查看 spoolFileName() 以获取本地磁盘上文件的文件名。也听一下 fileTooLarge(),因为它会通知你上传失败。

WFileUpload 的手册有很多信息和一个代码示例: http ://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WFileUpload.html

于 2013-04-24T08:56:21.283 回答
3

我意识到这是一篇旧文章,但我也遇到了问题,而且问题没有得到很好的回答(特别是读取文件内容所需的uploadFiles函数)

在您的构造函数(即 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;
}

我希望这可以帮助其他重定向到这里的人。

于 2015-02-02T03:00:04.653 回答