Forum: Ruby Sinatra files download

4719e9e0162789b66f578a7e7f530b26?d=identicon&s=25 Honza Hejzl (welblaud)
on 2014-08-19 10:17
Hi,

I really can't figure out why I am not able to download files from my
testing server.

I am able to upload file to the /files folder. Settings most close to
the problem:

  set :static, true
  set :public_dir, '/files'


  get '/files/:filename' do |filename|
    send_file('./files/#{filename}', :filename => filename, :type =>
'application/octet-stream')
  end

The link is shown well in the browser as well as it says "the proper
link" does not contain the file. I am pretty sure it contains. The
server shows error 404. It seems to me it is connected with some 'halt'
function but I can't figure out how to treat that.

Thanks in advance.
7e1614e9431deef2bf123693dd6bb59d?d=identicon&s=25 Vlad M_ (vladm)
on 2014-08-19 12:12
The `public_dir` settings seems to be incorrect, you have absolute path
there, I bet you have no '/files' on your machine.

Try this:

set :public_dir, File.expand_path(__dir__, 'files')

And the route:

get '/files/:filename' do |filename|
    send_file(filename, :filename=>filename,
:type=>'application/octet-stream')
end
4719e9e0162789b66f578a7e7f530b26?d=identicon&s=25 Honza Hejzl (welblaud)
on 2014-08-19 12:41
Yes!

Thank you!

In case you know how to download (not open) html file, let me know too
:o)
7e1614e9431deef2bf123693dd6bb59d?d=identicon&s=25 Vlad M_ (vladm)
on 2014-08-19 13:37
You need to set the http header Content-Disposition. Your route would
look like:

get '/files/:filename' do |filename|
  headers['Content-Disposition'] = "attachment;
filename=\"#{filename}\""
  send_file(filename)
end
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.