Over a million developers have joined DZone.

Sending Files With Sinatra

DZone's Guide to

Sending Files With Sinatra

· ·
Free Resource
How to send files (attachments) with Sinatra 0.9.4.

get '/some/:file' do |file|
  file = File.join('/some/path', file)
  send_file(file, :disposition => 'attachment', :filename => File.basename(file))

I found the documentation was misleading in some cases and plain wrong in others. The key lie is the method "signature":

  def send_file(path, opts={})

This is confusing because Sinatra only uses options if you specify the disposition to either "inline" or "attachment". Otherwise, it simply throws the File (which doesn't know/care about your options). It is not enough to simply call


Although the documentation says File.basename will be uses as the default name, that simply isn't true. Using Firefox, I got "download" as the filename.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}