使用wt上传文件

问题描述 投票:0回答:3

我是 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);
}
wt
3个回答
4
投票

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

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


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;
}

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


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.