Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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))
end


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


  send_file('/my/file')


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.
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}