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:
我正在尝试通过连接到mongoose 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;
}
...
However, the response I get is just an encoded string: MHg3ZjRmYTk5Y2RhYjA=
, but I want the browser to download the zip file.
但是,我得到的响应只是一个编码字符串:MHg3ZjRmYTk5Y2RhYjA =,但我希望浏览器下载zip文件。
Its worth noting that I am using an older version of mongoose and I can't seem to find any solutions in the examples.
值得注意的是,我使用的是较旧版本的mongoose,我似乎无法在示例中找到任何解决方案。
1 个解决方案
#1
0
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.
希望这很有帮助。
#1
0
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.
希望这很有帮助。