# Downloading a file using ruby

**URL:** <https://rubytalk.org/t/downloading-a-file-using-ruby/52716>\
**Category:** ruby-talk\
**Created:** [7 April 2009 08:48 UTC](https://rubytalk.org/t/downloading-a-file-using-ruby/52716 "2009-04-07T08:48:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Idealone\_Ideally](https://avatars.discourse-cdn.com/v4/letter/i/3e96dc/32.png) [@Idealone\_Ideally](https://rubytalk.org/u/Idealone_Ideally)\
**Post date:** [7 April 2009 08:48 UTC](https://rubytalk.org/t/downloading-a-file-using-ruby/52716/1 "2009-04-07T08:48:36Z")

</div>

Hi All,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I want to download a file from my web application using/calling  
ruby code.  
Manually i can download reports by clicking on 'DOWNLOAD' button in my  
app, which in turn gets the file from servlet path(where the files are  
stored).  
My question is, how can i automate this download part in ruby, if its  
feasible.  
&nbsp;&nbsp;&nbsp;&nbsp;During my googling, i found a set of ruby code to download pictures  
from a site.

snippet of the code looks like this:  
\<CODE\>  
require 'net/http'

Net::HTTP.start("[static.test.com](http://static.test.com)") { |http|  
&nbsp;&nbsp;resp = http.get("/92/2134322cedc5fef7\_o.jpg")  
&nbsp;&nbsp;open("fun.jpg", "wb") { |file|  
&nbsp;&nbsp;&nbsp;&nbsp;file.write(resp.body)  
&nbsp;&nbsp;&nbsp;}  
}  
\<CODE\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Please let me know if i could use similar technique to download a  
file(excel file).

Thanks

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![bwv549](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@bwv549](https://rubytalk.org/u/bwv549)\
**Post date:** [7 April 2009 21:34 UTC](https://rubytalk.org/t/downloading-a-file-using-ruby/52716/2 "2009-04-07T21:34:08Z")

</div>

require 'open-uri'

&nbsp;&nbsp;open("[http://localhost/jtprince/TRASH.TRASH](http://localhost/jtprince/TRASH.TRASH)") do |io|  
&nbsp;&nbsp;&nbsp;&nbsp;data = io.read  
&nbsp;&nbsp;end

Are you looking for something like that? This works on my own machine  
to grab files under the web server.

--John

---

<div class="post-metadata">

**Author:** ![bwv549](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@bwv549](https://rubytalk.org/u/bwv549)\
**Post date:** [7 April 2009 21:34 UTC](https://rubytalk.org/t/downloading-a-file-using-ruby/52716/3 "2009-04-07T21:34:09Z")

</div>

yes, this should download any kind of file (including excel). I use  
it to read/write mp3 files from online:

require 'open-uri'  
open("[http://someplace.com/somefile.mp3](http://someplace.com/somefile.mp3)") do |in\_io|  
&nbsp;&nbsp;File.open("somefile.mp3", 'w') do |out\_io|  
&nbsp;&nbsp;&nbsp;&nbsp;out\_io.print in\_io.read  
&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

**Author:** ![Idealone\_Ideally](https://avatars.discourse-cdn.com/v4/letter/i/3e96dc/32.png) [@Idealone\_Ideally](https://rubytalk.org/u/Idealone_Ideally)\
**Post date:** [8 April 2009 13:14 UTC](https://rubytalk.org/t/downloading-a-file-using-ruby/52716/4 "2009-04-08T13:14:47Z")

</div>

bwv549 wrote:

> **···**
>
> > yes, this should download any kind of file (including excel). I use  
> > it to read/write mp3 files from online:
> > 
> > require 'open-uri'  
> > open("[someplace.com](http://someplace.com/somefile.mp3&quot;%5C)) do |in\_io|  
> > &nbsp;&nbsp;File.open("somefile.mp3", 'w') do |out\_io|  
> > &nbsp;&nbsp;&nbsp;&nbsp;out\_io.print in\_io.read  
> > &nbsp;&nbsp;end  
> > end
> 
> ----------------------------------------------------------------
> 
> Thanks guys for the reply.  
> &nbsp;&nbsp;&nbsp;Actually i had mistaken that the download picks the file from web  
> server, but after talking to my developers i realized that they are  
> internally getting the file using ftp.  
> &nbsp;&nbsp;&nbsp;I wanted to know how can i "pull a file from ftp". Is there any ruby  
> library to pull or push files through "ftp"
> 
> Cheers  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [8 April 2009 13:25 UTC](https://rubytalk.org/t/downloading-a-file-using-ruby/52716/5 "2009-04-08T13:25:58Z")

</div>

Open-uri also works with ftp uri's  
[http://www.cs.tut.fi/~jkorpela/ftpurl.html](http://www.cs.tut.fi/~jkorpela/ftpurl.html)

> **···**
>
> On Wed, Apr 8, 2009 at 9:14 AM, Idealone Ideally \<shekarls@gmail.com\> wrote:
> 
> > bwv549 wrote:  
> > \> yes, this should download any kind of file (including excel). I use  
> > \> it to read/write mp3 files from online:  
> > \>  
> > \> require 'open-uri'  
> > \> open("[someplace.com](http://someplace.com/somefile.mp3&quot;%5C)) do |in\_io|  
> > \> File.open("somefile.mp3", 'w') do |out\_io|  
> > \> out\_io.print in\_io.read  
> > \> end  
> > \> end
> > 
> > ----------------------------------------------------------------
> > 
> > Thanks guys for the reply.  
> > &nbsp;&nbsp;Actually i had mistaken that the download picks the file from web  
> > server, but after talking to my developers i realized that they are  
> > internally getting the file using ftp.  
> > &nbsp;&nbsp;I wanted to know how can i "pull a file from ftp". Is there any ruby  
> > library to pull or push files through "ftp"
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
