RoR:如何从 ActiveStorage API 端点下载文件
RoR: How to download a file from ActiveStorage API endpoint
我正在尝试使用 RoR 进行 API 休息,我是 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
我不能再进一步了。根据documentation我可以用download来有个文件。但是我不能让它工作。
那么,请问如何继续前进?
您可以通过send_data
发送
send_data(open(path.file_element.url).read, type: path.file_element.content_type)
我正在尝试使用 RoR 进行 API 休息,我是 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
我不能再进一步了。根据documentation我可以用download来有个文件。但是我不能让它工作。
那么,请问如何继续前进?
您可以通过send_data
send_data(open(path.file_element.url).read, type: path.file_element.content_type)