access image file from the server side (http_listener) .

Jun 11, 2014 at 1:33 PM
i am trying to access the data of an image file uploaded by a client .
here is the code .
auto data = req.extract_vector().get();
FILE* out = NULL;
if(!(out = fopen("a.jpg","wb+")))
    return false;
fwrite(&data[0],1,data.size()*sizeof(uchar),out);
fclose(out);

but the uploaded file is not a valid image file .
here is the client code ( in C# if it helps ) .
                    WebClient client = new WebClient();
                    Uri uri = new Uri(@"http://localhost:8085");
                    byte[] ret = client.UploadFile(uri, "C:\\a.jpg");
                    Console.WriteLine(ret.ToString());
Jun 11, 2014 at 9:48 PM
Trying to help but I only have example of opposite direction. The concepts are similar. This is slightly Windows C++ specific.
    FILE *f;
    errno_t e = fopen_s(&f, fn.c_str(), "rb"); // fn is filename in string_t
    if (e)
        return(web::http::status_codes::NotFound);
    else
    {
        if (fseek(f, 0L, SEEK_END) == -1L)
            return(web::http::status_codes::LengthRequired);
        size_t l = ftell(f);
        if (l == -1L)
            return(web::http::status_codes::LengthRequired);
        if (fseek(f, 0L, SEEK_SET) == -1L)
            return(web::http::status_codes::LengthRequired);
        std::vector<unsigned char> v(l);
        if (l != std::fread(&v[0], 1, v.size(), f))
            return(web::http::status_codes::LengthRequired);
        fclose(f);
        web::http::http_response r(web::http::status_codes::OK);
        r.headers().add(U("Content-Type", U("image/gif"); // needed for browser to display image, otherwise notifies of download (tested on Firefox)
        r.set_body(v);
        message.reply(r);
Jun 12, 2014 at 2:52 PM
Edited Jun 12, 2014 at 2:53 PM
changing the C# code did the job for me !
        byte[] bytes = File.ReadAllBytes(file_name);
        WebClient client = new WebClient();
        byte[] ret = client.UploadData("http://localhost:8085", bytes);
        Console.WriteLine(Encoding.Default.GetString(ret));