forum & binary cgi

J

Jaap

I too am new to Ruby, but i love it! Two questions though:

1. I read and printed a png image from disk and printed it to the
browser but it doesnt't work:

print "Content-type: image/png\n\n"
file = File.new(imagePath, 'r')
content = ''
file.each_line {|line| content += line }
file.close
print content

Could this have anything to do with binary mode? How could i get this to
work?

2. What is the forum/community site where most Ruby experts hang out?
Like Perl has perlmonks.org

Thanks in advance,

Teun van Eijsden
The Netherlands
 
G

Gavin Sinclair

I too am new to Ruby, but i love it! Two questions though:
1. I read and printed a png image from disk and printed it to the
browser but it doesnt't work:
print "Content-type: image/png\n\n"
file = File.new(imagePath, 'r')
content = ''
file.each_line {|line| content += line }
file.close
print content
Could this have anything to do with binary mode? How could i get this to
work?

Try this:

print "Content-type: image/png\n\n"
File.open(image_path, 'rb') do |file|
content = file.read
end # file is auto-closed here
print content

Explicitly closing a file is *so* last-century ;)

You can use 'content = File.read(image_path)' but I'm not sure if
that's read as text or binary.
2. What is the forum/community site where most Ruby experts hang out?
Like Perl has perlmonks.org

No real forum to speak of. That's what ruby-talk is for.

Check out the Ruby Documentation Bundle (ruby-doc.org/downloads) for
some meaty documentation and a newbie's guide to the Ruby community.
Summary: ruby-talk, IRC, rubygarden.org, Wiki, RAA, RubyForge.

Cheers,
Gavin
 
J

Jaap

Gavin said:
Try this:

print "Content-type: image/png\n\n"
File.open(image_path, 'rb') do |file|
content = file.read
end # file is auto-closed here
print content

Thank you very much. It works if i add STDOUT.binmode
Now if i can only get those damn multipart file uploads working...

Teun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top