错误:如何从ActiveStorage API端点下载文件



我正在尝试RoR制作API Rest,我是ActiveStorage的新手,我能够使用它上传文件。现在我正在努力下载那个文件。

我有这个模型:

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

和这个控制器:

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

我不能再走了。根据文件,我可以使用下载有一个文件。但是我不能让它工作。

那么,请问,如何从这里继续下去?

您可以通过send_data发送

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

最新更新