Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I download a file over HTTP using Ruby?

Tags:

How do I download a file over HTTP using Ruby?

like image 506
Markus Avatar asked Dec 09 '09 21:12

Markus


People also ask

How do you download a file in Ruby?

Plain old Ruby The most popular way to download a file without any dependencies is to use the standard library open-uri . open-uri extends Kernel#open so that it can open URIs as if they were files. We can use this to download an image and then save it as a file.

How do I download a file from HTTP?

In general, downloading a file from an HTTP server terminal via HTTP GET consists of the following steps: Make an HTTP GET request to send to the HTTP server. Send an HTTP request and receive an HTTP response from the HTTP server. Save the contents of the HTTP response file to a local file.

How do I download a file in rails?

For Example: i Have my file in SVG folder inside Public Directory. Now we Can Access any file in Public Folder Like below and Pass id and Download option. Download option rename any file which u want to download. Now Click able link is Ready We Can Click on Above link to Download the File.


2 Answers

Probably the shortest way to download a file:

require 'open-uri' download = open('http://example.com/download.pdf') IO.copy_stream(download, '~/my_file.pdf') 
like image 102
Clemens Helm Avatar answered Sep 28 '22 06:09

Clemens Helm


You can use open-uri, which is a one liner

require 'open-uri'  content = open('http://example.com').read 
like image 21
KrauseFx Avatar answered Sep 28 '22 04:09

KrauseFx