如何将从文件上传小部件上传的假脱机文件传递到 C FILE *f 指针

Posted

技术标签:

【中文标题】如何将从文件上传小部件上传的假脱机文件传递到 C FILE *f 指针【英文标题】:How to pass a spooled file, uploaded from a file upload widget, to a C FILE *f pointer 【发布时间】:2012-08-20 08:35:13 【问题描述】:

我需要将一些从 Wt 文件上传小部件上传的文本文件作为参数传递给某些 C 函数以处理其数据。除非我真的找不到使用假脱机文件名(从 std::string 转换为 char *)作为第一个 fopen() 的参数来打开文件的正确方法,否则我意识到我没有理解Wt提供的文件处理方法。即,假脱机文件存储在哪里?我尝试在文件上传完成后使用std::string WtFileUpload::spoolFileName() const(我提供了一个当进度条到达末尾时可以点击的按钮),但是通过挖掘errno编号,我发现文件不存在。

请给我建议一个正确的方法来处理这个问题(如果可以的话,提供一些代码)并帮助我理解 Wt 处理使用这个小部件上传的文件的方式。

提前致谢。

UPDATE:我的文件上传和处理代码:

//Declared in the application class:
Wt::WFileUpload *RORUpload;
Wt::WPushButton *RORUploadButton;

//Create file upload widget and add handlers for it in application constructor:
root()->addWidget(new Wt::WText("RINEX File Loaders "));
new Wt::WBreak(root());
//RINEX observation data file from rover station uploader.
new Wt::WBreak(root());
RORUpload = new Wt::WFileUpload();
RORUpload->setFileTextSize(40);
root()->addWidget(new Wt::WText("RINEX Rover Observation Data "));
new Wt::WBreak(root());
RORUploadButton = new Wt::WPushButton("Upload", root());
root()->addWidget(RORUpload);
//Upload when the button is clicked.
RORUploadButton->clicked().connect(RORUpload, &Wt::WFileUpload::upload);
RORUploadButton->clicked().connect(RORUploadButton, &Wt::WPushButton::disable);
// Upload automatically when the user entered a file.
RORUpload->changed().connect(RORUpload, &Wt::WFileUpload::upload); // !!! do not rename this 'upload'
RORUpload->changed().connect(RORUploadButton, &Wt::WPushButton::disable);
// React to a succesfull upload.
RORUpload->uploaded().connect(RORUpload, (Wt::WObject::Method) &Wt::WFileUpload::uploaded);
//React to a fileupload problem.
RORUpload->fileTooLarge().connect(RORUpload, (Wt::WObject::Method) &Wt::WFileUpload::fileTooLarge);

//When user presses a "submit" button, a connection function will be called.
const std::string Nameofror = RORUpload->spoolFileName();
const char *CNameofror = Nameofror.c_str();

我想知道问题是否与我的 (Wt::WObjectMethod) 演员表有关。 编辑:查看我对这个问题的评论。

更新 2:忘了说当我点击“提交”按钮时我遇到了程序崩溃 代码:

//Create button into application constructor:
  SubmitButton = new Wt::WPushButton("Submit", root());
SubmitButton->clicked().connect(SubmitButton, &Wt::WPushButton::disable);
SubmitButton->clicked().connect(this, &GOPApplication::definedOptions);

//definedOptions:

void GOPApplication::definedOptions()


int Posmode = PTGroup->checkedId();  //Value selection from PTContainer (checked radio button id).

     //int singlemoving;                                              //Value selection from SPContainer (checked radio button id).
int Frequencies = FGroup->checkedId();                  //Value selection from FContainer (checked radio button id).
const Wt::WString WElmask = EMedit->text();             //Value selection from EMw (given number).
std::string SElmask = WElmask.narrow();                 //Value selection from EMw (given number).
const char *CElmask = SElmask.c_str();                  //Value selection from EMw (given number).
float Elmask = atof(CElmask);                           //Value selection from EMw (given number).
int Height = HGroup->checkedId();                       //Value selection from HContainer (checked radio button id).
int Soltype = STGroup->checkedId();                     //Value selection from STContainer (checked radio button id).
int Sateph = SAGroup->checkedId();                      //Value selection from SEContainer (checked radio button id).
int Basestatcoord = BSCGroup->checkedId();              //Value selection from BSCContainer (checked radio button id).
int Ionerror = IEGroup->checkedId();                    //Value selection from IEContainer (checked radio button id).
int Troperror = TEGroup->checkedId();                   //Value selection from TEContainer (checked radio button id).
int Receiverapcpvc = RAGroup->checkedId();              //Value selection from RAContainer (checked radio button id).
int Satelliteapvc = SAGroup->checkedId();               //Value selection from SAContainer (checked radio button id).
int Earthtidescorr = ETCGroup->checkedId();             //Value selection from ETCContainer (checked radio button id).
int Ambresstr = STRGroup->checkedId();                  //Value selection from STRContainer (checked radio button id).
const Wt::WString WMinlockcount = MLCedit->text();      //Value selection from MLCw (given number).
std::string SMinlockcount = WMinlockcount.narrow();     //Value selection from MLCw (given number).
const char *CMinlockcount = SMinlockcount.c_str();      //Value selection from MLCw (given number).
float Minlockcount = atof(CMinlockcount);               //Value selection from MLCw (given number).
const Wt::WString WMinfixcount = MFCedit->text();       //Value selection from MFCw (given number).
std::string SMinfixcount = WMinfixcount.narrow();       //Value selection from MFCw (given number).
const char *CMinfixcount = SMinfixcount.c_str();        //Value selection from MFCw (given number).
float Minfixcount = atof(CMinfixcount);                 //Value selection from MFCw (given number)
const Wt::WString WThreshold = Tedit->text();           //Value selection from Tw (given number).
std::string SThreshold = WThreshold.narrow();           //Value selection from Tw (given number).
const char *CThreshold = SThreshold.c_str();            //Value selection from Tw (given number).
float Threshold = atof(CThreshold);                     //Value selection from Tw (given number).
const Wt::WString WMinelevangle = MEAedit->text();      //Value selection from MEAw (given number).
std::string SMinelevangle = WMinelevangle.narrow();     //Value selection from Tw (given number).
const char *CMinelevangle = SMinelevangle.c_str();      //Value selection from Tw (given number).
float Minelevangle = atof(CMinelevangle);               //Value selection from Tw (given number).
int Settimesystem = STSGroup->checkedId();              //Value selection from STSContainer (checked radio button id).
int Outputresiduals = ORGroup->checkedId();             //Value selection from ORContainer (checked radio button id).
int Settimeformat = STFGroup->checkedId();              //Value selection from STFContainer (checked radio button id).
//What about RINEX files obtaining?
//Maybe... (see notebook for infos about spoolFileName() usage)
const std::string Nameofror = RORUpload->spoolFileName();
const char *CNameofror = Nameofror.c_str();
printf("\n\n============= %s =============\n\n", CNameofror);
//const std::string Nameofrob = ROBUpload->spoolFileName();
//const char *CNameofrob = Nameofrob.c_str();
//const std::string Nameofrn = RNUpload->spoolFileName();
//const char *CNameofrn = Nameofrn.c_str();
//post-processing(Posmode,Frequencies,Elmask,Height,Soltype,Sateph,Basestatcoord,Ionerror,Troperror,Receiverapcpvc,
//              Satelliteapvc,Earthtidescor,Ambresstr,Minlockcount,Minfixcount,Threshold,Minelevangle,Settimesystem,Outputresiduals,Settimeformat);
 

【问题讨论】:

我的恐惧似乎得到了保证。错误源于我对RORUpload->uploaded().connect(RORUpload, (Wt::WObject::Method) &Wt::WFileUpload::uploaded); 操作的误解。 connect() 的第二个参数必须是我自己实现的信号处理程序,它处理uploaded 信号。 fileTooLarge() 也是如此。 【参考方案1】:

您确定文件上传过程完成了吗?如果它是真的,那么你应该只在你的代码中收到信号WFileUpload::uploaded()WFileUpload::fileTooLarge() 之后才尝试使用文件。请检查一下。

【讨论】:

我在文件上传小部件创建后和上传完成后放置WFileUpload::uploaded()(我用进度条小部件检查这一点)我指示用户单击一个按钮,将文件上传与处理程序连接起来函数(试图将上传的文件传递给 C 文件指针的函数)。我认为我观察到的故障不是由我的代码中的函数放置引起的,而是由于对它们的操作的误解。我将编辑我的帖子以提供相关代码。【参考方案2】:

如果您使用函数 FileIsLoad() 连接上传的信号并且您的 Wt::WFileUpload 对象是 Upload

void MyClass::FileIsLoad ( void ) 
     
       auto Res = std::make_shared<Wt::WFileResource>( 
                       Upload->uploadedFiles()[0].contentType() ,                                            
                       Upload->uploadedFiles()[0].spoolFileName() ) ;
       string MyFile = string("\mydirectory\" ) +
                       Upload->uploadedFiles()[0].clientFileName( ) ;

       std::ofstream Stream ( MyFile           , 
                              std::ios::out    | 
                              std::ios::binary | 
                              std::ios::trunc  ) ;
       Res->write            ( Stream          ) ;
       Stream.close          (                 ) ;
     

上传的文件会保存在\my目录中,与原文件同名。

【讨论】:

以上是关于如何将从文件上传小部件上传的假脱机文件传递到 C FILE *f 指针的主要内容,如果未能解决你的问题,请参考以下文章

如何以闪亮的方式遍历多个上传小部件?

如何在使用 Cloudinary UI 小部件时跳过 Cloudinary 弹出窗口进行上传

如何配置 Kendo 上传小部件以隐藏文件列表

如何在 sql*plus 中修复我的假脱机 csv 文件中的格式

AppMaker 和选择器小部件 - 上传到特定文件夹

将 BinaryField 的文件上传小部件添加到 Django Admin