如何直接下载pdf文件,而不是使用rails中的carrierwave将其重定向到新页面



我正在使用carrierwave上传和下载文件。我可以直接下载所有的文件。但在pdf文件的情况下,当我点击下载时,它会重定向到新页面,要求打开、下载等字段。有没有办法直接下载pdf文件?(这是我的客户要求)

我的控制器:

  def show
 @invoice_detail = InvoiceDetail.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @invoice_detail }
    format.pdf { render :layout => false }
  end
  end

attachment_uploader.rb:

 def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  def extension_white_list
    %w(xlsx pdf doc htm html docx png jpg jpeg xls)
  end
  def filename
  "#{model.invoice_number}.#{file.extension}" if original_filename.present?
end

show.html.erb:

<%= link_to 'download file', invoice_details.attachment_url %>

在show.html.erb 中使用以下行

<%= link_to 'download file', invoice_details.attachment_url, download: invoice_details.attachment_url, target: "_blank" %>

以上仅适用于HTML5。更多信息http://www.w3schools.com/jsref/prop_anchor_download.asp

编辑:如果你想删除打开/下载提示,这是不可能的。这是浏览器的配置。

最新更新