从猫鼬服务器下载 tar.gz

Downloading a tar.gz from a mongoose server

我正在尝试通过连接到猫鼬 Web 服务器的 Web GUI 下载压缩文件。我认为天真的方法是将 ifstream 传递到 ostream 响应中:

...
std::stringstream zip_location;
zip_location << path << "/" << timestamp << ".tar.gz";
std::ifstream zip_file(zip_location.str());

if(zip_file){
    // Enters here fine
    request.set_response_content_type("application/x-tar-gz");
    request.add_response_header(new http_header_fields::cache_control_no_cache);
    request.out() << zip_file;
    return true;
}
...

然而,我得到的响应只是一个编码字符串:MHg3ZjRmYTk5Y2RhYjA=,但我希望浏览器下载 zip 文件。

值得注意的是,我使用的是旧版本的猫鼬,我似乎无法在 examples.

中找到任何解决方案

事实证明 request class 中确实实现了一个虚拟函数,它确实下载了一个文件。您需要做的就是传递一个与文件路径相对应的字符串。

if(zip_file){
    request.set_response_content_type("application/x-tar-gz");
    request.add_response_header(new http_header_fields::cache_control_no_cache);
    request.send_file(zip_loc.str());
    return true;
}

希望这对您有所帮助。