c++httpmongoose-web-server

Downloading a tar.gz from a mongoose server


I'm trying to download a zipped file through a web GUI connected to a mongoose web server. I figured the naive approach would be to pass the ifstream into the ostream response:

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

However, the response I get is just an encoded string: MHg3ZjRmYTk5Y2RhYjA=, but I want the browser to download the zip file.

Its worth noting that I am using an older version of mongoose and I can't seem to find any solutions in the examples.


Solution

  • Turns out there is indeed a virtual function implemented in the request class that does download a file. All you need to do is pass a string corresponding to the path of the file.

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

    Hopefully this is helpful.