From: Ryan Leavengood Date: 2005-04-21T05:12:54+09:00 Subject: Re: Newb CGI Question > So I thought I had CGI in Ruby figured out, but I'm stumped on the > following: > > I want use Ruby to dynamically serve an image. The image tag would > look like , where 200 is the id for an > image. > > So here's what I have (I'm trading embarrassment for guidance): > > require "cgi" > cgi = CGI.new("html4") > cgi.out('image/png') > File.read('200.png') > > Now, I'm pretty sure I'm not reading the file right, but what I seem > to be stuck on first is getting my MIME type right. When hitting the > CGI, I can't get Content-Type to come back as 'image/png'. It's always > 'text/html'. > > Any tips? Try the following: require "cgi" cgi = CGI.new("html4") cgi.out('image/png') do File.read("#{cgi['id']}.png") end You have to supply the content in a block passed to out. I also added the necessary code to read the id and open that file. Ryan Leavengood