Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails send_file rendering files as plain text on new page instead of browser downloading file

When send_file is called, it's sending the file to the browser, but the browser is dumping the contents as plain-text on a new page instead of downloading the file. If I refresh that page, it then downloads the file as normal.

Route

get 'download' => 'qr_codes#download'

Controller

def download
    path = Rails.root.join("events/active/#{params[:name]}/#{params[:batch]}/#{params[:file]}")
    send_file(path, type: 'application/vnd.ms-excel', filename: params[:file])
end

View

<%= link_to 'Original Upload', download_path(name: @event_name,
  batch: batch, file: batch_upload_filename(@event_name, batch)) %>

SOLUTION: This ended up being a known issue with turbolinks. If using Turbolinks 5 like I am, the updated syntax is: data: { turbolinks: false }

like image 847
Matt Weick Avatar asked Dec 06 '25 07:12

Matt Weick


2 Answers

This ended up being a known issue with turbolinks. If using Turbolinks 5 like I am, the updated syntax is:

data: { turbolinks: false }
like image 165
Matt Weick Avatar answered Dec 07 '25 22:12

Matt Weick


Try setting the disposition:

def download
  path = Rails.root.join("events/active/#{params[:name]}/#{params[:batch]}/#{params[:file]}")
  send_file(path, type: 'application/vnd.ms-excel', filename: params[:file], disposition: 'attachment')
end

Or changing the file to ensure the extension is correct

"#{params[:file][0,params[:file].index(".")]}.xlsx"

Oh and don't inject params into a string to build routes for downloading. I can inject "../../" into :name, "config", into :batch, and "../config/database.yml" into :file. Add the file path to a model.

like image 25
Andy Gauge Avatar answered Dec 07 '25 22:12

Andy Gauge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!