You can only send one response at a time, but you are trying to send back two responses smashed together (one blob of text and one image):
print "Content-type: text/plain\n\n";
print p( "A OK" );
print "Content-type: image/png\n\n";
Only the first header is interpreted as a valid header, and hence the browser thinks it is seeing one big text response.
What you must do is send back one response per request. The trick is to set the src attribute of your first response's IMG element in such a way as to cause the client's browser to call your CGI back and ask for the image.
This is the idea:
- The client's web browser requests your page.
- Your CGI receives request and sends back a text/html response. The HTML includes an IMG element whose src attribute points back to your CGI and
passes in an "I want the image" parameter.
- The client's browser receives your response, parses the HTML, and then tries to load the image. In doing so, it calls your CGI again (the src attribute points to it), asking for the image.
- Your CGI checks the request parameters and notices the "I want an image" parameter. The CGI then generates the image and sends back an image/png (or whatever type is appropriate) response.
- The client receives the image and finishes rendering the page.
- It's happy time. Dance.
For a complete example that uses this technique, see the script referenced at the end of The Mobile Weather Problem and its solution on my site.
Cheers,
Tom
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.