ruby-on-rails - RoR:如何从 ActiveStorage API 端点下载文件
问题描述
我正在尝试 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
我不能再进一步了。根据文档,我可以使用下载来获取文件。但我无法让这个工作。
那么,请问,如何从这里继续前进?
解决方案
你可以通过发送send_data
send_data(open(path.file_element.url).read, type: path.file_element.content_type)
推荐阅读
- firebase - Flutter Firebase Analytics 事件丢失
- java - 您已经在存储库中定义了查询方法,但您没有定义任何查询查找策略。春天
- git - 如何使用语义版本构建 Go Module?
- c# - 不要在应用程序洞察中记录静态文件的遥测数据
- ruby-on-rails - 无法使用指南添加新路线
- java - 如何使用 GSON 编辑/更改 JSON 文件中的值?
- c++ - 缺少符号——但符号存在吗?
- sql - 将数据插入没有唯一列的 SQL Server 表时避免重复
- python - 使用 python 和 BigQuery API 获取 BigQuery 数据集中的表列表
- node-red - 是否可以在 Deploy on Node-RED 上关闭长时间运行的进程的实例?