[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help, how to download a binary file through Net::HTTP

Eric Luo

6/4/2005 5:01:00 AM

Hi,
I'm new to Ruby. I want to download a binary file by Net::HTTP or other
ways. I mimic the
what's been done in the "Programming Ruby". But It failed. I want to know
what the
right way is!
Thanks
3 Answers

Mark Hubbart

6/4/2005 6:25:00 AM

0

On 6/3/05, Eric Luo <eric.wenbl@gmail.com> wrote:
> Hi,
> I'm new to Ruby. I want to download a binary file by Net::HTTP or other
> ways. I mimic the
> what's been done in the "Programming Ruby". But It failed. I want to know
> what the
> right way is!

The quick way, if you just want the file contents:

require 'net/http'
data = Net::HTTP.get(URI.parse('http://www.google.com/index...))

... data now has the contents of the file. Then there's the slightly
more involved way, which lets you reuse the http connection (if you
want to fetch more than one file), and get the headers, too:

google = Net::HTTP.new('www.google.com', 80)
headers, data = google.get 'index.html'

Net::HTTP#get (not Net::HTTP.get) returns an array, the first part is
the header info, the second is the file data.

hth,
Mark


Erik Veenstra

6/4/2005 8:51:00 AM

0

> I'm new to Ruby. I want to download a binary file by
> Net::HTTP or other ways. I mimic the what's been done in the
> "Programming Ruby". But It failed. I want to know what the
> right way is!

You can't download a file. But you can download the contents of
a file and write to a file yourself. And the latter might be a
problem if you open the file with "w" instead of "wb" for
binary files.

gegroet,
Erik V. (http://www.erikve...)

Martin DeMello

6/4/2005 7:05:00 PM

0

Erik Veenstra <pan@erikveen.dds.nl> wrote:
> > I'm new to Ruby. I want to download a binary file by
> > Net::HTTP or other ways. I mimic the what's been done in the
> > "Programming Ruby". But It failed. I want to know what the
> > right way is!
>
> You can't download a file. But you can download the contents of
> a file and write to a file yourself. And the latter might be a
> problem if you open the file with "w" instead of "wb" for
> binary files.

A #download(source, dest) might not be such a bad addition to the API,
at that.

martin