Solved

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

Posted on 1997-12-08
5
340 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
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 …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

733 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