使用 cpprest (Casablanca) 返回 PDF 响应

Use cpprest (Casablanca) to return PDF response

本文关键字:返回 PDF 响应 Casablanca cpprest 使用      更新时间:2023-10-16

我在Ubuntu Linux上的服务器中使用cpprest。到目前为止,我能够处理请求,并使用 JSON 响应进行回复。

我接受的请求之一需要使用 PDF 文件进行响应。我看到http_request类有一个接受异步流的 reply(( 方法。对于我的一生,我不知道如何将此流与磁盘上的PDF文件相关联。

utility::string_t pdfFilename = getPdfFilename();
concurrency::streams::istream stream; // how do associate my pdfFilename?
request.reply(web::http::status_codes::OK, stream, "application/pdf");

我希望你已经想通了。这是我使用本地pdf文件回复的方式

void replyPdf(web::http::http_request message, string_t file_name)
{
concurrency::streams::fstream::open_istream(file_name, std::ios::in)
.then([=](concurrency::streams::istream is)
{
web::http::http_response response(web::http::status_codes::OK);
response.headers().add(L"Content-Disposition", U("inline; filename = "") + file_name + U("""));
response.set_body(std::move(is), U("application/pdf"));
message.reply(response).then([](pplx::task<void> t) {});
});
}