开发者

Displaying mixed content in a dynamically generated html page

I have two .png files that I would like to display along with some text in an html page that is being dynamically generated by Perl. In a perfect world this would be in a 3 column layout with the text in the first column and the two .png files in the second and third columns. The problem I keep coming across is the need for one of these tags:

print "Content-type: image/png\n\n";
print "Content-type: text/html\n\开发者_开发技巧n";

I can only include one in the html and it means I can only display the text or the images but not both.

Obviously there must be a way to do this but so far Googling has gotten me nowhere.

Any advice is appreciated.

Regards.


Here are the print statements that I am using to generaate the dynamic html:

   print "<html>";
   print "<head>";
   print "Content-type: text/html\n\n";
   print "</head>";
   print "<body>";
   print "<table>";
   print "<tr><td>Number of Sentences = $numSentences</td><td rowspan=\"4\"><img src=\"/home/kfarmer/public_html/kevin/charFreq.png\" /></td><td rowspan=\"4\"><img src=\"/home/kfarmer/public_html/kevin/charFreq.png\" /></td></tr>";
   print "<tr><td>Number of Words = $numWords</td></tr>";
   print "<tr><td>Number of Unique Words = $numUniqueWords</td></tr>";
   print "<tr><td>Number of English Characters = $numEnglishCharacters</td></tr>";
   print "</table></body></html>";

However, now when I generate this page I get a popup asking me how I would like to open the file -- and the file is just what I've printed above.


One HTTP response sends one thing, and each thing gets its own content header. You don’t need to include everything that will be in the page in one response.

From Perl, just link to the image files using the HTML features for that. In the HTML, you’d include:

 <img src="url to PNG"/>

If there’s a CGI script serving up a dynamic image, you’d just link to it in the HTML:

 <img src="/cgi-bin/imager.png?key=value;..."/>

That CGI script has to return the proper content type for what it’s sending back, whether HTML or image data.

The browser will interpret the HTML, see the links for the images, and make additional requests to get the images.


Your problem is more likely, that your images are absolute paths on the drive, rather than relative paths to the images on your websever.

I noticed that your img src is '/home/kfarmer/public_html/kevin/charFreq.png'

So, if your webserver's root is '/home/kfarmer/public_html/kevin' and that is what will load when you go to someurl.com, then your img src should just be '/charFreq.png'

If the root of your server is '/home/kfarmer/public_html' then your img src should be 'kevin/charFreq.png'

This is more complicated than that, as urls are relative to where the page is loaded from.

So, if the page is www.someurl.com/kevin/anotherfolder/mypage.html and you want to load an image in 'kevin/someotherplace/charFreq.png' then you need your img src to be either '/kevin/someotherplace/charFreq.png' OR '../someotherplace/charFreq.png'


I am afraid you are mixing up some definitions/technologies.

Context-type is part of the HTTP header. When your browser fetches a web page it normally uses HTTP, which consists of multiple header fields and a data block (for example containing the data which got requested). If you use a (decent) web-server you do not need to worry about HTTP headers (normally).

In order to show a 3 column page, with two (or any other number of) images and some text. You need to generate a HTML page. On this page you tell the browser that it should download the images and present them alongside the text.

In order to do, so you have to generate the HTML page in a pre-defined format containing so called elements:

<html>
  <head><title>mypage</title></head>
  <body>
    This is the visible part of the page
  </body>
</html>

Now in the body element, you can generate your text and images. The images can be placed onto the page using the img element.

Check out w3schools for more extensive tutorials on these subjects. If you gained some more basic knowledge, you can try to generate a HTML page using perl (or any other (scripting) language).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜