如何保存 poco 上传的文件

How to save poco uploaded file

本文关键字:文件 poco 何保存 保存      更新时间:2023-10-16

我想使用 poco 库上传文件。

现在我在 istream 变量中有上传的文件,但我不知道如何将其保存到文件中?

这是我的代码,我可以在其中获取文件的长度。

void handlePart(const MessageHeader &header, std::istream &stream) {
    _type = header.get("Content-Type", "(unspecified)");
    if (header.has("Content-Disposition")) {
        std::string disp;
        NameValueCollection params;
        MessageHeader::splitParameters(header["Content-Disposition"], disp, params);
        _name = params.get("name", "(unnamed)");
        _fileName = params.get("filename", "(unnamed)");
    }
    CountingInputStream istr(stream);
    NullOutputStream ostr;
    StreamCopier::copyStream(istr, ostr);
    _length = istr.chars();

}

此外,现在它是在表单中上传的 1 个文件,如果有多个文件 如何在 istream 中管理它?

现在有 2 天我正在寻找和测试不同的方法,但我找不到任何方法,请帮助解决此问题。

提前谢谢你。

取决于这篇文章 #Abhinav 问题:

我们可以像这样编写保存代码:

if (fileName.length() != 0){
        std::ofstream fout( fileName, std::ios::binary);
        fileList.push_back(fileName);
        fout << stream.rdbuf() ;

        fout.close();
    }

但不幸的是,如果有多个文件,则此代码无法正确捕获,则它适用于一个文件。