返回二进制缓冲区的uWebSockets 我使用UWebSosiCsC++试图返回二进制缓冲区。当我执行res->end(…)时,它发送空响应,下面是我的代码: .get("/binaryfile", [](auto *res, auto *req) { const char * fname = "../server/test.png"; FILE* filp = fopen(fname, "rb" ); if(!filp) { res->end("File not found!"); } else { std::cout << "File found" << std::endl; char * buffer = new char[BUFFERSIZE]; std::string result; size_t bt = 0; size_t total_size = 0; while ( (bt = fread(buffer, sizeof(char), BUFFERSIZE, filp)) > 0 ) { result.append(buffer); total_size = total_size + bt; } // Done and close. fclose(filp); std::string_view bd(result.c_str(), total_size); res->writeHeader("Content-Type", "image/png"); std::cout << bd <<std::endl; res->end(bd); } .get(“/binaryfile”,[](自动*res,自动*req){ const char*fname=“../server/test.png”; 文件*filp=fopen(fname,“rb”); 如果(!filp){ res->end(“未找到文件!”); }否则{ std::cout writeHeader(“内容类型”、“图像/png”); 标准::cout主机:192.168.0.104:3000 >用户代理:curl/7.58.0 >接受:*/* >

返回二进制缓冲区的uWebSockets 我使用UWebSosiCsC++试图返回二进制缓冲区。当我执行res->end(…)时,它发送空响应,下面是我的代码: .get("/binaryfile", [](auto *res, auto *req) { const char * fname = "../server/test.png"; FILE* filp = fopen(fname, "rb" ); if(!filp) { res->end("File not found!"); } else { std::cout << "File found" << std::endl; char * buffer = new char[BUFFERSIZE]; std::string result; size_t bt = 0; size_t total_size = 0; while ( (bt = fread(buffer, sizeof(char), BUFFERSIZE, filp)) > 0 ) { result.append(buffer); total_size = total_size + bt; } // Done and close. fclose(filp); std::string_view bd(result.c_str(), total_size); res->writeHeader("Content-Type", "image/png"); std::cout << bd <<std::endl; res->end(bd); } .get(“/binaryfile”,[](自动*res,自动*req){ const char*fname=“../server/test.png”; 文件*filp=fopen(fname,“rb”); 如果(!filp){ res->end(“未找到文件!”); }否则{ std::cout writeHeader(“内容类型”、“图像/png”); 标准::cout主机:192.168.0.104:3000 >用户代理:curl/7.58.0 >接受:*/* >,c++,uwebsockets,C++,Uwebsockets,正如您所看到的,就在res->end(bd)之前,有一个std::无法取得任何进展吗? > GET /binaryfile HTTP/1.1 > Host: 192.168.0.104:3000 > User-Agent: curl/7.58.0 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: image/png < uWebSockets: 18 < Content-Length: 30

正如您所看到的,就在
res->end(bd)
之前,有一个
std::无法取得任何进展吗?
> GET /binaryfile HTTP/1.1
> Host: 192.168.0.104:3000
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: image/png
< uWebSockets: 18
< Content-Length: 3097
< 
Warning: Binary output can mess up your terminal. Use "--output -" to tell 
Warning: curl to output it to your terminal anyway, or consider "--output 
Warning: <FILE>" to save to a file.
* Failed writing body (0 != 1365)
* stopped the pause stream!
* Closing connection 0