Link to home
Start Free TrialLog in
Avatar of manuelf
manuelf

asked on

How to display a GIF/JPEG file from a CGI in visualc++?

I am writing a really small cgi in visualc++ that has the only purpose of displaying a gif (or jpeg) file.

I read the file in binary and then I send the "Content-Type: image/gif" header followed by the binary data.

When I run the cgi from Internet Explorer it doesn't display the image correctly. It does display an image but it is way different from the original image (looks like noise).

I wonder if anyone can help me out here. I don't know what the problem is, since the content type header is OK and I think I am reading the file correctly (I write an output file that looks exactly the same as the original file).

I have found perl scripts that do this but I need to do it in visualc++ (windows).
ASKER CERTIFIED SOLUTION
Avatar of faster
faster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aurelio
aurelio

If nothing works , I suggest you coding it in normal c AND without using streams (it's possible to do it in vc++)
perhaps there are problems with ios::binary , buffering or so.
Inspect the output with pc-tools or debug and be sure it is what you expect.
If it dont work , post another comment and i´ll send you a C program.
hasta luego lucas!
Avatar of manuelf

ASKER

I did what you said but it didn't work. Actually, I had already tried that before. Any other suggestions?
The program listed below SHOWS the picture contained in "TIA1.GIF" ( current dir ) , and works in VC 5.0 :

#include <windows.h>
#include <iostream.h>
#include <fstream.h>


void saca(char *s)
{
        cout << "Content-type: text/html\n\n" ;
        cout << "<html>\n<head><title>ruina</title>";
        cout << "</head>\n<body>\n<h1>" << s << "\n";
        cout << "</h1>\n</body>\n</html>\n" ;
}

void main(int argc, char **argv)
{ char *buf = new char [ 256 ];
ifstream fich("TIA1.GIF",ios::in|ios::binary|ios::nocreate);

  if(fich.bad())
  { saca("OPEN FILE ERROR");exit(1);
  }
  cout << binary;
  cout << "HTTP/1.0 200 OK\n" ;
  cout << "Content-type: image/gif\n\n" ;

  while(!fich.eof())
  { fich.read(buf,255);
    cout.write(buf,255);      # chapuza : better fich.gcount() |
  }
  cout << text ;
  delete [] buf ;
  exit(0);
 }

Avatar of manuelf

ASKER

I would like to thank Aurelio a lot for the c program he added as a comment, for it worked perfectly and saved my life.

Aurelio, muchísimas gracias por el programa que me diste, me salvaste la vida. Te doy mi mail para estar en contacto: alfonsof@reforma.com.mx. Saludos, paisano!!!