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

E

Eric Luo

------=_Part_7782_3446521.1117861248954
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

------=_Part_7782_3446521.1117861248954--
 
M

Mark Hubbart

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 =3D Net::HTTP.get(URI.parse('http://www.google.com/index.html'))

... 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 =3D Net::HTTP.new('www.google.com', 80)
headers, data =3D 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
 
E

Erik Veenstra

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.erikveen.dds.nl/)
 
M

Martin DeMello

Erik Veenstra said:
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
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top