Table of contents
1.
Introduction 
2.
Sending Files in Sinatra
2.1.
Example 1
2.1.1.
Output 
2.2.
Optional Parameters of send_file method
2.2.1.
:filename
2.2.2.
:type
2.2.3.
:last_modified
2.2.4.
:length
2.2.5.
:disposition
2.2.6.
:status
2.3.
Example 2
2.3.1.
Output
3.
Frequently Asked Questions
3.1.
What is Sinatra?
3.2.
What is a helper method in Sinatra?
3.3.
How is Sinatra different from Ruby on Rails?
3.4.
What is RubyGems in Ruby?
3.5.
What does the disposition parameter do in the send_file helper method of Sinatra?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Sending Files in Sinatra

Author Teesha Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

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'
end
You can also try this code with Online Ruby Compiler
Run Code

Output 

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.
 

Output Command Line

 

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

Output screen

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'
end
You can also try this code with Online Ruby Compiler
Run Code

Output

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

Error Page

Frequently Asked Questions

What is Sinatra?

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.

What is a helper method in Sinatra?

A helper method is a pre-defined method that helps perform a particular task. It has the instructions written already, and we have to invoke the function without understanding the underlying details of how it is executing. 

How is Sinatra different from Ruby on Rails?

Sinatra is more robust; you can build more customized web applications with Sinatra compared to Ruby on Rails. Hence with Sinatra, you have to work more, requiring more development time. 

What is RubyGems in Ruby?

RubyGems is a standard distribution mechanism for Ruby applications and libraries. It's a Ruby programming language package manager. Since Ruby version 1.9.c, RubyGems has been included in the standard library.

What does the disposition parameter do in the send_file helper method of Sinatra?

The disposition parameter is used to set 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. 

Conclusion

This article has discussed how to send files in Sinatra. We have also illustrated the concept of sending files with the help of an example.

I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Thank You

Live masterclass