?
Solved

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

Posted on 1997-12-08
5
Medium Priority
?
346 Views
Last Modified: 2013-12-25
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).
0
Comment
Question by:manuelf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
faster earned 20 total points
ID: 1831590
You only should set the Content_Length header field and there must be an empty line between the header fields and the body, which is in your case the binary data.
0
 

Expert Comment

by:aurelio
ID: 1831591
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!
0
 

Author Comment

by:manuelf
ID: 1831592
I did what you said but it didn't work. Actually, I had already tried that before. Any other suggestions?
0
 

Expert Comment

by:aurelio
ID: 1831593
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);
 }

0
 

Author Comment

by:manuelf
ID: 1831594
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!!!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question