Sinatra files download

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.

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

Yes!

Thank you!

In case you know how to download (not open) html file, let me know too
:o)

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