Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

2. Fundamentals > Attachments

Attachments

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.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial