JPEG to Browser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redvi4
    New Member
    • Jan 2007
    • 5

    JPEG to Browser

    Hello Friends THis is my first Post
    i like to program on php/unix, perl

    But at this time i have a question.

    i want to make an .exe program that make only
    1 action or work:

    read a JPEG file and send to the browser.

    pd:
    When i change image/jpeg and file to read to
    text/html , html file, the program run and display the html template

    Operative System: Windows XP
    Web Server: Local Apache win32 server
    Using Borland Compiler



    --
    PROGRAM

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <sys/stat.h>
    struct stat results;
    
    using namespace std;
    
    
    int main () {
       string s, line;
       if (stat("d:\\curl\\tmp\\xantatosa.jpg", &results) == 0)
       {
    
          cout <<"Content-Length: "<<results.st_size;
    
       }
    
       cout << "\n";
       cout << "Content-Type: image/jpeg";
    
    
       cout << "\n\n";
       //ifstream myfile ("d:\\panzona\\tmp\\fotogata.jpg", ios:: out | ios::in | ios::binary);
       fstream myfile("d:\\panzona\\tmp\\fotogata.jpg",ios:: out | ios::in | ios::binary);
       char ch;
       myfile.seekg(ios::beg); //go to the beginning of the file
    
       while (! myfile.eof() )
       {
          myfile.get(ch); //read one character
          cout << ch ; //display it
       }
       myfile.close();
    
    }
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Originally posted by redvi4
    Hello Friends THis is my first Post
    i like to program on php/unix, perl

    But at this time i have a question.

    i want to make an .exe program that make only
    1 action or work:

    read a JPEG file and send to the browser.

    pd:
    When i change image/jpeg and file to read to
    text/html , html file, the program run and display the html template

    Operative System: Windows XP
    Web Server: Local Apache win32 server
    Using Borland Compiler



    --
    PROGRAM

    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <sys/stat.h>
    struct stat results;

    using namespace std;


    int main () {
    string s, line;
    if (stat("d:\\curl \\tmp\\xantatos a.jpg", &results) == 0)
    {

    cout <<"Content-Length: "<<results.st_s ize;

    }

    cout << "\n";
    cout << "Content-Type: image/jpeg";


    cout << "\n\n";
    //ifstream myfile ("d:\\panzona\\ tmp\\fotogata.j pg", ios:: out | ios::in | ios::binary);
    fstream myfile("d:\\pan zona\\tmp\\foto gata.jpg",ios:: out | ios::in | ios::binary);
    char ch;
    myfile.seekg(io s::beg); //go to the beginning of the file

    while (! myfile.eof() )
    {
    myfile.get(ch); //read one character
    cout << ch ; //display it
    }
    myfile.close();

    }
    What is your question?

    Comment

    • redvi4
      New Member
      • Jan 2007
      • 5

      #3
      My question is
      "what is the correct C++ / C Code to present the jpeg file to browser"

      :( with my program , only display "cant show image" on the browser

      Thanks

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by redvi4
        My question is
        "what is the correct C++ / C Code to present the jpeg file to browser"

        :( with my program , only display "cant show image" on the browser

        Thanks
        Is your program giving you any errors? What happens when you run it?

        Comment

        • redvi4
          New Member
          • Jan 2007
          • 5

          #5
          Is your program giving you any errors? What happens when you run it?
          My program doesnt have compilation errors, my program have parse error with JPEG image, cos when i run the program on my webserver, it load but dont show the image.

          When i try with "text/html" and parse HTML file, the program works fine.
          When i change to "image/jpg", and try to parse JPEG file with the program on the ITEM #1 the program compile, but there is a parse error, cos the image is not show
          :(

          Comment

          • redvi4
            New Member
            • Jan 2007
            • 5

            #6
            :( any suggestion?

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              Originally posted by redvi4
              :( any suggestion?
              You may want to try "\r\n" instead of simply "\n".
              That's my only idea.

              Comment

              • drhowarddrfine
                Recognized Expert Expert
                • Sep 2006
                • 7434

                #8
                Actually it should be done twice /r/n/r/n and, yes, /n won't work at all.

                Comment

                • nmadct
                  Recognized Expert New Member
                  • Jan 2007
                  • 83

                  #9
                  The OP actually did include two newlines after the HTTP headers. And it doesn't have to be "\r\n"; although "\r\n" is the network-standard line terminator; "\n" works just fine for me. It's something wacky about the usage of the C++ output stuff, I think. I wrote this using C I/O and it worked great:

                  Code:
                  #include <stdio.h>
                  #include <sys/stat.h>
                  
                  int main (int argc, char *argv[]) {
                   char fileBuffer[1024];
                   struct stat results;
                  
                   /* If stat can fetch the file info, then send the
                      content length to the browser. */
                  
                   int fileLen = -1;
                   if (stat("smile.jpg", &results) == 0)
                    fileLen = results.st_size;
                  
                   FILE *myFile = fopen("smile.jpg", "rb");
                   if (myFile==NULL)
                   {
                    printf("Content-Type: text/plain\n\nError.\n");
                    return 1;
                   }
                  
                   if (fileLen != -1)
                    printf("Content-Length: %d\n", results.st_size);
                   printf("Content-Type: image/jpeg\n\n");
                  
                   while (!feof(myFile))
                   {
                    size_t nRead = fread(fileBuffer, 1, 1024, myFile);
                    size_t nWritten = fwrite(fileBuffer, 1, nRead, stdout);
                    if (nWritten != nRead) {
                     /* error */
                     return 1;
                    }
                   }
                  
                   fclose(myFile);
                  }
                  Sorry, I don't normally use C++ I/O so I'm not sure what could be going wrong.

                  Comment

                  • Motoma
                    Recognized Expert Specialist
                    • Jan 2007
                    • 3236

                    #10
                    What happens when you run the program not from a web browser?

                    Comment

                    • nmadct
                      Recognized Expert New Member
                      • Jan 2007
                      • 83

                      #11
                      The output of your C++ code is nearly identical to my C code. The difference is that your code repeats the last byte of the JPEG file, so it comes out one byte longer. That must be enough to make the browser consider the file corrupted and refuse to display it. I think the problem is in your I/O loop. You read a byte, then write it, then check for EOF. In the final iteration of the loop, when you've actually reached EOF, the final character you write isn't valid because you've passed the end of the file. So, rewrite the I/O loop like this:

                      Code:
                      myfile.get(ch);
                      while (!myfile.eof())
                      {
                        cout << ch;
                        myfile.get(ch);
                      }
                      I tested it and it works for me.

                      One more thing. You don't need this line:

                      Code:
                      myfile.seekg(ios::beg); //go to the beginning of the file
                      When you open the file you're already at the beginning, you don't need to tell it to go there.

                      Comment

                      • Motoma
                        Recognized Expert Specialist
                        • Jan 2007
                        • 3236

                        #12
                        Originally posted by nmadct
                        The output of your C++ code is nearly identical to my C code. The difference is that your code repeats the last byte of the JPEG file, so it comes out one byte longer. That must be enough to make the browser consider the file corrupted and refuse to display it. I think the problem is in your I/O loop. You read a byte, then write it, then check for EOF. In the final iteration of the loop, when you've actually reached EOF, the final character you write isn't valid because you've passed the end of the file. So, rewrite the I/O loop like this:

                        Code:
                        myfile.get(ch);
                        while (!myfile.eof())
                        {
                          cout << ch;
                          myfile.get(ch);
                        }
                        I tested it and it works for me.

                        One more thing. You don't need this line:

                        Code:
                        myfile.seekg(ios::beg); //go to the beginning of the file
                        When you open the file you're already at the beginning, you don't need to tell it to go there.
                        Damn good eye nmadct!

                        Comment

                        • drhowarddrfine
                          Recognized Expert Expert
                          • Sep 2006
                          • 7434

                          #13
                          The OP actually did include two newlines after the HTTP headers. And it doesn't have to be "\r\n"; although "\r\n" is the network-standard line terminator; "\n" works just fine for me.
                          The protocol requires \r\n. It's been a long time since I've forgotten to include it but it does fail on the net. iirc, the page won't display without it.

                          Comment

                          • nmadct
                            Recognized Expert New Member
                            • Jan 2007
                            • 83

                            #14
                            Originally posted by drhowarddrfine
                            The protocol requires \r\n. It's been a long time since I've forgotten to include it but it does fail on the net. iirc, the page won't display without it.
                            It worked fine with \n when I tried it yesterday, serving the page via Apache/CGI to both Firefox and IE6 on a different machine. However, you're right that the standard network line ending is \r\n, and there's no reason not to follow the standard even if most browsers won't care either way.

                            Comment

                            • redvi4
                              New Member
                              • Jan 2007
                              • 5

                              #15
                              THIS IS MY last Modded Code:


                              #include <iostream.h>
                              #include <fstream.h>
                              #include <string.h>
                              #include <sys/stat.h>
                              struct stat results;

                              using namespace std;


                              int main () {
                              string s, line;
                              if (stat("d:\\lava platito.jpg", &results) == 0)
                              {
                              cout <<"Content-Length: "<<results.st_s ize;
                              }
                              cout << "\n";
                              cout << "Content-Type: image/jpeg";
                              cout << "\r\n\r\n";
                              //ifstream myfile ("d:\\curl\\tmp \\xantatosa.jpg ", ios::out | ios::in | ios::binary);
                              fstream myfile("d:\\lav aplatito.jpg",i os::out | ios::in | ios::binary);
                              char ch;
                              // myfile.seekg(io s::beg); //go to the beginning of the file
                              myfile.get(ch);
                              while (!myfile.eof())
                              {
                              cout << ch;
                              myfile.get(ch);
                              }
                              myfile.close();

                              }

                              BUT STILL DOesnt Work:
                              Note: I use Borland BCC55 command Line for Windows
                              and apache for windows
                              OS : windows :(

                              nmadct can u show your code to try to compile con my PC
                              ;)

                              thnks

                              Comment

                              Working...