3

I have the following in my .erb:

<%= link_to 'Download PDF', planners_download_pdf_url %>

I have the following method to respond:

  def download_pdf
  send_file(
     "#{Rails.root}/Thing/ex/example.xls",
filename: "mything.xls",
type: "application/xls"
  )
  end

My routes has:

get '/planners/download_pdf'

When I click my link, the method does get invoked. My problem is that is doesn't download to the browser. Instead, it takes the tab I have open in the browser and dumps the file as HTML. Like... have you ever seen an Excel file opened in notepad? It does that. See below:

Garbage

How do I get this to instead download the file?

PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
  • I hope this helps you.
    [https://stackoverflow.com/questions/13164063/file-download-link-in-rails](https://stackoverflow.com/questions/13164063/file-download-link-in-rails)
    – Jim Tebstone Aug 04 '17 at 15:07
  • What happens if you use `send_data`? Also, you're being inconsistent with `pdf` vs `xls` :) – Tom Lord Aug 04 '17 at 15:07

1 Answers1

15

You need to add disposition: 'attachment' to your send_file option hash (or at least ensure that you do not have disposition: 'inline' set, since 'attachment' should be the default).

If this is not the issue, if you are using Turbolinks, as mentioned in other answers you need to disable Turbolinks on the link by setting data-turbolinks="false" in your link element (e.g.: data: {turbolinks: false} in your link_to tag helper).

Finally, here are some other things to try if this doesn't work:

  • Set type to 'application/vnd.ms-excel', the valid MIME type for XLS files.
  • Set the download="mything.xls" html5 attribute on the link tag directly (e.g.: download: 'mything.xls' in your link_to tag helper.
wjordan
  • 19,770
  • 3
  • 85
  • 98
  • I actually changed the method to: send_file( "#{Rails.root}/Planners/viacom/Viacom_TVE_March_2017_PAIDs_03102017.xls", filename: "mything.xls", type: "application/xls", disposition: 'attachment' ) And still get this behavior. – PinkElephantsOnParade Aug 04 '17 at 15:12
  • 1
    if you are using Turbolinks, this may be the cause of the issue, and you can try disabling that in your link tag. – wjordan Aug 04 '17 at 15:15
  • I am not using turbolinks in this case. It's just a vanilla erb link_to. I will check the duplicate question, however. – PinkElephantsOnParade Aug 04 '17 at 15:17
  • Interestingly, if I refresh the page, it downloads the file... but still keeps the garbage text output there. Need to see if it is simply rendering the wrong page on method invocation? Not sure what to do with it. I see the similarities for the duplicate question, but the answer I am not able to readily apply to my own program. – PinkElephantsOnParade Aug 04 '17 at 15:23
  • I have the exact same problem, I'm using rails 6. I set the disposition to 'attachment' too. When I reload the page it downloads, but not when I hit the link. I also have a link to the same controller function in an email and that works fine. Just the link on the page doesn't work. – Gold Masta Oct 09 '20 at 23:41
  • Using Rails 6 I had to add the `data: { turbolinks: false }` for it to work for me. – covard Dec 12 '22 at 19:04
  • In Rails 7 it's `data: { turbo: false }` – Robert Jun 16 '23 at 09:40