Sending Files With Sinatra
Join the DZone community and get the full member experience.
Join For FreeHow 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.
Sinatra (software)
Opinions expressed by DZone contributors are their own.
Comments