venkat
venkat

Reputation: 836

How to direct download the pdf file instead of redirecting it to new page using carrierwave in rails

I am using carrierwave for uploading and downloading the files. I am able to download all the files directly. But in case of pdf files, when I click on download it is redirecting to new page asking for open with, download and etc fields. Is there any way to direct download pdf files? (which is my client requirement)

My controller:

  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 %>

Upvotes: 3

Views: 1990

Answers (1)

Hardik
Hardik

Reputation: 3895

Use following line in show.html.erb

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

Above only works with HTML 5. more info http://www.w3schools.com/jsref/prop_anchor_download.asp

EDIT: If you want to remove the open/download prompt than it's not possible. It's browser's configuration.

Upvotes: 2

Related Questions