سؤال

I am writing to write a cgi script to display a png image in a browser. When user clicks on the 'Submit' form in the HTML page , the CGI program is called to display the image .

But this is not working for me . The script is trying to open the PNG image in a browser,but the image contains the following error message

"The image “http://localhost/cgi-bin/image.sh?subbtn=Submit” cannot be  displayed, because it contains errors."

The following is the snippet of CGI code.

!/bin/bash

echo "Content-type: text/html"
echo "Content-type: image/png"

echo ""

echo  "<html>"
echo "<body>"
echo "Hi"

echo "<img src="/home/zaman/ssdggraph/SSDGhistory.png" alt="DG-Reports">"
echo "</body>"
echo "</html>"

If I remove the below line from the above code , then the text "DG-Reports" in alt section from img src tag is displayed.

echo "Content-type: image/png"

Also if I write the same code in plain HTML page , the PNG image is displayed fine.

Please suggest what I am missing in the code to display the image without any errors ?

هل كانت مفيدة؟

المحلول

You should specify a URL for the <IMG>'s src attribute that can be reached from the browser.

/home/zaman/ssdggraph/SSDGhistory.png is unlikely to to satisfy that requirement. (You should probably have someting like http://path_known_to_webserver/SSDGhistory.png).

You should probably also read up on the concepts of web pages and cgi scripts. I would call a CGI "that displays an image", some program that would be referenced in the above URL and dynamically streams out the image data once the web pages refers to it and a browser asks for it. That might be what you mean, but it is kind of unclear.

نصائح أخرى

You have to use content type, then blank line:

#!/bin/bash
echo "Content-type: image/png"
echo
cat /var/www/img/your-image.png
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top