尝试使用 send_file 强制下载(副打开)时获取 ActionController::MissingFile
Getting ActionController::MissingFile when trying to force a download (vice open) with send_file
我正在使用 rails 4.x 和回形针。我想确保当用户点击 link 下载回形针附件时,文件会被下载而不是打开。
我使用的 link 将根据浏览器配置打开或保存
<%= link_to image_tag("save.gif", document.doc_file.url, :target => "_blank" %>
link 有时会打开文件而不是下载。
我设置的方法
helper_method :download
def download
@document= Document.find(39)
send_file ("http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf?1458068410"),
:filename => @document.doc_file_file_name,
:type => @document.doc_file_content_type,
:disposition => 'attachment'
end
我硬连线 url 进行测试。我也尝试过 send_file ("http://localhost:3000#{@document.doc_file.url}")
和 send_file (@document.doc_file.url)
。都没有用。
我的 link 是
<%= link_to image_tag("save.gif"), download_path(document.id) %>
routes.rb 有
match 'documents/download/:id' => 'documents#download',via: [:get, :post], :as => 'download'
当我点击下载 link 时,出现
错误
ActionController::MissingFile in DocumentsController#download
Cannot read file http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf
Rails.root: C:/Users/cmendla/RubymineProjects/technical_library
Application Trace | Framework Trace | Full Trace
app/controllers/documents_controller.rb:17:in `download'
如果我将 URL 放入浏览器的地址栏中,它就可以工作。即`http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf'
send_file
方法接受文件 在您服务器上的 物理位置,而不是 public URL。所以像下面这样的东西应该可以工作,这取决于 PDF 文件实际所在的位置:
send_file "#{Rails.root}/public/images/39/Medical_Opportunity_EvaluationForm.pdf",
:filename => @document.doc_file_file_name,
:type => @document.doc_file_content_type,
:disposition => 'attachment'
如果这是回形针文档,path
方法应该有效:
send_file @document.doc_file.path,
:filename => @document.doc_file_file_name,
:type => @document.doc_file_content_type,
:disposition => 'attachment'
有关详细信息,请参阅 the docs。
我正在使用 rails 4.x 和回形针。我想确保当用户点击 link 下载回形针附件时,文件会被下载而不是打开。
我使用的 link 将根据浏览器配置打开或保存
<%= link_to image_tag("save.gif", document.doc_file.url, :target => "_blank" %>
link 有时会打开文件而不是下载。
我设置的方法
helper_method :download
def download
@document= Document.find(39)
send_file ("http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf?1458068410"),
:filename => @document.doc_file_file_name,
:type => @document.doc_file_content_type,
:disposition => 'attachment'
end
我硬连线 url 进行测试。我也尝试过 send_file ("http://localhost:3000#{@document.doc_file.url}")
和 send_file (@document.doc_file.url)
。都没有用。
我的 link 是
<%= link_to image_tag("save.gif"), download_path(document.id) %>
routes.rb 有
match 'documents/download/:id' => 'documents#download',via: [:get, :post], :as => 'download'
当我点击下载 link 时,出现
错误ActionController::MissingFile in DocumentsController#download
Cannot read file http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf
Rails.root: C:/Users/cmendla/RubymineProjects/technical_library
Application Trace | Framework Trace | Full Trace
app/controllers/documents_controller.rb:17:in `download'
如果我将 URL 放入浏览器的地址栏中,它就可以工作。即`http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf'
send_file
方法接受文件 在您服务器上的 物理位置,而不是 public URL。所以像下面这样的东西应该可以工作,这取决于 PDF 文件实际所在的位置:
send_file "#{Rails.root}/public/images/39/Medical_Opportunity_EvaluationForm.pdf",
:filename => @document.doc_file_file_name,
:type => @document.doc_file_content_type,
:disposition => 'attachment'
如果这是回形针文档,path
方法应该有效:
send_file @document.doc_file.path,
:filename => @document.doc_file_file_name,
:type => @document.doc_file_content_type,
:disposition => 'attachment'
有关详细信息,请参阅 the docs。