Closed ezamosch closed 4 years ago
What browser's 'Inspector' shows? Maybe this will help: https://github.com/mrozigor/mrozigor.net/blob/master/src/controllers/ResourcesController.cpp
Solution is here:
void sendFile(crow::response& res, std::string filename, std::string contentType) {
std::string ROOT_DIR = "publics/";
std::string filename_path = ROOT_DIR.append(filename);
if (contentType == "images/jpeg") { //pictures
std::ifstream fileContent(filename_path, std::ios::binary);
if (fileContent.is_open()) {
res.add_header("Content-Type", contentType);
res.write(std::string(std::istreambuf_iterator<char>(fileContent), std::istreambuf_iterator<char>()));
}
else {
std::cerr << "Content not found..." << std::endl;
res.code = 404;
res.write("Content not found...");
}
}
else { //other stuff
std::string ROOT_DIR = "publics/";
std::string filename_path = ROOT_DIR.append(filename);
try {
std::ifstream InputContent;
InputContent.open(filename_path, std::ifstream::in);
InputContent.exceptions(std::ifstream::failbit | std::ifstream::badbit);
std::ostringstream stringStream;
stringStream << InputContent.rdbuf();
InputContent.close();
res.add_header("Content-Type", contentType);
res.write(stringStream.str());
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
res.code = 404;
res.write("Content not found...");
}
}
res.end();
}
Hello.
I am trying Crow and had a problem of transfer static pictures from server to client(browser).
My program looks like:
But on client (in browser) I got empty pictures. Can you help me with this issuee?