Re: m_HttpResponse does not show image using rdbuf()

Tech-Archive recommends: Fix windows errors by optimizing your registry



Igor,

m_HttpResponse.WriteStream((LPCSTR)is.rdbuf(), length, NULL);

How on earth do you expect this to work? Why do you believe it's OK to
cast filebuf* to char*?

You are sending contents of some random memory block to the client. You
haven't read a single byte of data from the actual file yet.

Thank you for the explanation. I studied the istream documentation further
this morning and now do see that it wouldn't have worked. I have a better
grasp now.

If you just want to send contents of a file on disk as is, see
CHttpResponse::TransmitFile.

Thanks for the tip.

Igor, the following seems to work fine and I think I have a good
understanding of what it's doing. I plan to create a CAPTCHA utility for my
website, thus I'd like an efficient way of showing an image. Does the
following code seem correct and efficient to you?

int length;
char * buffer;

ifstream is;
is.open ("C:/Inetpub/wwwroot/WebTests/img.jpg", ios::binary );

is.seekg (0, ios::end); // set position of the get pointer
length = is.tellg(); // get position of the get pointer

is.seekg (0, ios::beg); // moves the read position in a stream

buffer = new char [length];

// read block of data and close file
is.read (buffer,length);
is.close();

// display image via ATL Server
m_HttpResponse.WriteStream(buffer, length, NULL);
delete[] buffer;

Best Regards,

Jeff
.



Relevant Pages