From: Matthew Smillie Date: 2006-06-18T02:07:08+09:00 Subject: Re: Downloading Image On Jun 17, 2006, at 17:27, Doug Bromley wrote: > Hello again > > As an extension on my previous image problem I can't even successfully > download images from a URL location. I though tthe following > simple GET > request along with writing the result to a file would work but it > mangles > the image and makes it unreadable. > > Net::HTTP.start("www.example.com") do |http| > resp = http.get("/images/logo.gif") > outfile = open("C:/logo.gif", "w") > outfile.write(resp.body) > outfile.close > end > > Have I done something wrong here? Can anyone see what I'm doing > wrong? On Windows you need to write certain files in 'binary' mode. The appropriate mode strings are documented in the IO class: http://www.ruby-doc.org/core/classes/IO.html Also, the common idiom for writing is generally like this: open("somefile.etc", 'wb') { |file| file.write(...) } This is done in order to ensure that if there's an exception after you open the file (ferinstance, writing when the disk is full, or in a place you don't have write permissions), that the file is still closed. You could do the exception handling yourself, of course, but this is a convenient concise way of doing things. Hope that helps, matthew smillie.