Saturday 15 June 2013

ruby on rails3 retrieve files from a remote server and make it available for download -



ruby on rails3 retrieve files from a remote server and make it available for download -

i have files on remote server in directory such as:

class="lang-none prettyprint-override">171.xx.xx.xxx/test/data/file1.zip 171.xx.xx.xxx/test/data/file2.zip

so in web application, when user goes page (eg. www.mysite.com/downloads) want retrieve files remote server , create available user download.

so basically, webpage like

file1.zip file2.zip

and when user clicks on one, downloads remote files onto local computer.

what best way accomplish on ruby on rails?

edit: pointed out in comments create sense pass through server rather direct link remote server.

edit2: 'open-uri' work non http urls?

when tried

url_file_path = open('172xx.xx7/test/data/my_file.zip').read send_data url_file_path, filename: test.zip, type: 'application/octet-stream'

errno::enoent: no such file or directory

you need "download" remote file first. in controller

your_controller.rb

def download send_data remote_file(params[:url]), type: 'application/octet-stream' end private def remote_file(url) open(url).read end

you might need

require 'open-uri'

normally u @ top of of controller

now can define route

get 'download' => 'your_controller#download', as: :download

now can utilize in frontend

= link_to 'download file1.zip', download_path(url: 'http://171.xx.xx.xxx/test/data/file1.zip')

ruby-on-rails ruby

No comments:

Post a Comment