【发布时间】:2020-01-06 16:49:22
【问题描述】:
我有用于保存、打开、下载文件(例如:pdf、docx 等)的控制器。我将它们保存在 S3 上。但我不知道,我怎样才能下载或打开文件。 我的控制器
class PdfsController < ApplicationController
before_action :set_pdf, only: %i[show edit update destroy download]
before_action :set_url_file, only: %i[download show]
def show
send_file(@pdf_filename, filename: @pdf.path.to_s,
type: 'application/pdf',
x_sendfile: true,
disposition: 'inline')
end
def download
send_file(@pdf_filename, filename: @pdf.path.to_s,
type: 'application/pdf',
x_sendfile: true)
end
private
def set_url_file
if Rails.env.production?
@pdf_filename = File.join(@pdf.path.to_s)
else
@pdf_filename = File.join(Rails.root, "public#{@pdf.path}")
end
end
end
action show 在浏览器中显示 pdf 文件(仅 pdf),action download 在本地保存任何文件。
当我在本地保存文件时它起作用了。但是如果我在 heroku 上部署项目,我会收到错误 ActionController::MissingFile。在我知道我可以下载文件后,我必须使用aws-sdk。但我不明白它是如何用于打开和下载的。我该怎么做?
【问题讨论】:
标签: ruby-on-rails amazon-s3 aws-sdk