Solved

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

Posted on 1997-12-08
5
333 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
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
faster earned 10 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now