Home > Blockchain >  RoR: How to download a file from ActiveStorage API endpoint
RoR: How to download a file from ActiveStorage API endpoint

Time:10-23

I'm trying RoR to make an API Rest and I'm very new to ActiveStorage and I was able to upload a file using it. Now I'm struggling with it to download that file.

I have this model:

class Path < ApplicationRecord
  validates :name, presence: true, uniqueness: true
  validates :size, numericality: { greater_than: 0 }, presence: true
  has_one_attached :file_element, dependent: :destroy
end

and this controller:

class Api::V1::PathsController < ApplicationController
...
def download
   path = User.find(params[:id])
   binary = path.file_element.download
   # stucked here
end
...
end

I can't go further. According to the documentation I can use download to have a file. But I can't get this working.

So, please, how to keep going from here?

CodePudding user response:

You can send it through send_data

send_data(open(path.file_element.url).read, type: path.file_element.content_type)
  • Related