Introduction
Sinatra is an open-source web application framework of Ruby. It is an alternative to Ruby on Rails among the other web application frameworks based on Ruby. It is more robust and hence requires more work and time for development.
This article will discuss how to send files in Sinatra. We will illustrate the concept with the help of an example.

Sending Files in Sinatra
If you wish to get the contents of a file as a response, you can use the send_file helper method in Sinatra. A helper method is a pre-defined method that helps perform a specific task. It has the instructions written already, and we have to invoke the function without understanding the underlying details of how it is executing.
Example 1
Create a demo.rb file as follows:
require 'sinatra'
get '/' do
send_file 'Coding_Ninjas_Logo.png'
endOutput
Run ruby demo.rb on the command line after making sure you are in the same directory as where the demo.rb file is saved. You will see the following results.

Now visit http://localhost:4567, and you will see the below page.

Optional Parameters of send_file method
You can pass several parameters or options to the send file method. All of these parameters are optional.
:filename
You can specify the file name you want to use in the response using this parameter. By default, the filename is the same as the original file’s name.
:type
This parameter is used to set the value of the Content-Type header. By default, the type value is guessed by the file extension. You can also specify the type.
:last_modified
The last_modified parameter is used to set the value of the Last-Modified header. By default, it is the same as the file's mtime.
:length
It sets the value of the Content-Length header. By default, it is the same as the size of the file sent.
:disposition
It specifies the value of the Content-Disposition header. The possible values for d :disposition parameters are: nil, :attachment, and :inline. By default, it is set to nil.
:status
Using this option, you can specify the status code you want to send with the file. It can be used to send a static file as an error page.
Example 2
In this example, we will be sending an error image. First, we create an Error image we want to send, then using the helper method send_file, we send the image to the response.
Create an error_file.rb as shown below:
require 'sinatra'
get '/' do
send_file 'error_image.png'
endOutput
Visit http://localhost:4567, and you will see the below page.






