Hanami 1.3 > Hanami 2.2 : tips & notes about my journey

Use case #10: streaming a file like unsafe_send_file in Hanami 1

After a convoluted attempt to imitate it in Hanami 2, it appears that simply using the same method unsafe_send_file, but on the response object, works pretty well.

For example:

module MySlice
  module Actions
    module Home
      class Index < MySlice::Action
        def handle(request, response)
          response.unsafe_send_file("/tmp/your_file.xlsx")
        end
      end
    end
  end
end

Be careful that if you use a relative path, it is relative to the file of the action you’re writing, not to the project root.