eidheim / Simple-Web-Server

A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio. Created to be an easy way to make REST resources available from C++ applications.
MIT License
2.61k stars 751 forks source link

How to return to the image stream #85

Closed xmh0511 closed 7 years ago

xmh0511 commented 7 years ago

Hi,I want to make my server return the image stream ,I just write the code like this:"stringstream buf; buf << request->content.rdbuf(); response << "HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin:\r\nContent-Type:application/x-jpg" << "\r\n\r\n"<<buf.str();" but it's not work,thanks for giving me some help

eidheim commented 7 years ago

You seem to be missing the Content-Lenght header field?

xmh0511 commented 7 years ago

thank you,I have solved this problem code: try { auto ifs = make_shared(); ifs->open("D:/VC\boostServer\boostServer\web\img.jpg", ifstream::in | ios::binary); if (*ifs) { ifs->seekg(0, ios::end); auto length = ifs->tellg();

            ifs->seekg(0, ios::beg);

            *response << "HTTP/1.1 200 OK\r\nContent-Length: " << length <<"\r\nAccess-Control-Allow-Origin:*Content-Type:application/x-jpg"<< "\r\n\r\n";
            default_resource_send(server, response, ifs);
        }
    }catch(const exception &e){

    }