我正在嘗試 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
我不能更進一步。根據檔案,我可以使用下載來獲取檔案。但我不能讓它作業。
那么,請問,如何從這里繼續前進?
uj5u.com熱心網友回復:
你可以通過發送 send_data
send_data(open(path.file_element.url).read, type: path.file_element.content_type)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/332953.html
標籤:红宝石轨道 接口 休息 下载 rails-activestorage
