Вопрос

After I upload a file, I am trying to print out its spoolFileName() but although the application runs smoothly it seems as if the string of the name is empty. Any idea where it is wrong? (It's not the size of the file, it's less than 50k)

#include <Wt/WApplication>
#include <Wt/WFileUpload>
#include <Wt/WProgressBar>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/Http/Request>
#include <Wt/WString>

using namespace Wt;

class HelloApplication: public WApplication {
public:
     HelloApplication(const WEnvironment& env);

private:

     WPushButton *uploadButton;
     Wt::WFileUpload *fu;
 Wt::WString g;

     void greet();
 void fileUploaded();
};

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();
    fu->uploaded().connect(this, &HelloApplication::fileUploaded);
    g = fu->spoolFileName();
}

void HelloApplication::fileUploaded(){                              // application title
    root()->addWidget(new WText(g.value()));  
}

WApplication *createApplication(const WEnvironment& env) {

    return new HelloApplication(env);
}

int main(int argc, char **argv) {
    return WRun(argc, argv, &createApplication);
}
Это было полезно?

Решение

I think the filename for the spool file is only known after the file is uploaded. Move

g = fu->spoolFileName();

to HelloApplication::fileUploaded().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top