Return files from rails

Question with beginner rails: How to return a file from the controller to the rails?

I am familiar with returning / rendering JSON objects. However, I never returned / did not display a file with an arbitrary extension.

From reading around SO it is like render :nothing => true. I'm just looking for guidance or related documentation.

+5
source share
1 answer

You can use the built-in rails send_file or send_data .

To transfer the file (for example, for the endpoint of the file proxy), use send_file:

send_file("#{RAILS_ROOT}/path/to/file/on/server",
  :filename => "client-suggested-filename",
  :type => "mime/type")

For streaming data generation (for example, for generated PDF) use send_data:

send_data(your_data,
  :filename => "client-suggested-filename",
  :type => "mime/type")

mime , , , . , mime application/pdf, :filename something.pdf.

, mime , wikipedia mime-types gem. (, , mime, ).

+10

All Articles