Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
As demonstrated in Example 2-36, sending attachments to
clients is extremely easy in Sinatra; there is a built-in attachment method that optionally takes a
filename parameter. If the filename has an extension (.jpg, .txt,
etc.), that extension will be used to determine the Content-Type header for the response. The
evaluation of the route will provide the contents of the
attachment.
Example 2-36. Sending an attachment to a client
require 'sinatra' before do content_type :txt end get '/attachment' do attachment 'name_your_attachment.txt' "Here's what will be sent downstream, in an attachment called 'name_your_attachment.txt'." end
As always, we’ll take a look at the output via cURL. You’ll note the
addition of the Content-Disposition
header, which denotes both the fact that it contains an attachment and the
optional filename parameter.