Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 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

12 Experts available now in Live!

Get 1:1 Help Now