0

For whatever reason, my send_file doesn't actually prompt the user to download the file. However, If I right click on the link that triggers the send_file action, then the user is prompted to download the file. It just doesn't prompt it on the same page as the link.

Not sure why this is happening, but here's my action in the controller:

  # GET /consultants/agents/1/download_ssh_key
  def download_ssh_key
    @agent = Agent.find(params[:agent_id])
    send_file "/home/shared/agent_#{@agent.uuid}"
  end

When I click this link in the browser (without opening a new tab), this is what I see in the console:

I, [2019-10-05T22:40:47.713625 #3211]  INFO -- : [1f170528-920b-4330-9d17-7107220235dd] Started GET "/consultants/agents/9/download_ssh_key" for 66.195.29.70 at 2019-10-05 22:40:47 -0500
I, [2019-10-05T22:40:47.714597 #3211]  INFO -- : [1f170528-920b-4330-9d17-7107220235dd] Processing by Consultants::AgentsController#download_ssh_key as HTML
I, [2019-10-05T22:40:47.714673 #3211]  INFO -- : [1f170528-920b-4330-9d17-7107220235dd]   Parameters: {"agent_id"=>"9"}
D, [2019-10-05T22:40:47.717697 #3211] DEBUG -- : [1f170528-920b-4330-9d17-7107220235dd]   Agent Load (0.5ms)  SELECT  `agents`.* FROM `agents` WHERE `agents`.`id` = 9 LIMIT 1
I, [2019-10-05T22:40:47.719397 #3211]  INFO -- : [1f170528-920b-4330-9d17-7107220235dd] Sent file /home/shared/agent_22ac92d1 (0.3ms)
I, [2019-10-05T22:40:47.720488 #3211]  INFO -- : [1f170528-920b-4330-9d17-7107220235dd] Completed 200 OK in 6ms (ActiveRecord: 0.5ms)

Am I missing something in my send_file action that is supposed to trigger a download prompt? According to https://apidock.com/rails/ActionController/Streaming/send_file, the default option is to send it as an attachment (prompt for download) rather than loading the file in the browser.

halfer
  • 19,824
  • 17
  • 99
  • 186
LewlSauce
  • 5,326
  • 8
  • 44
  • 91
  • Does it help if you specify an http content type? https://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file – trueinViso Oct 06 '19 at 04:43
  • From the logs, it looks like rails is doing the right thing and the browser is not. Does that happen on any browser? what happen if the file has an extension? Do you see any warning o error on the browser's console or network tab on dev tools? – arieljuod Oct 06 '19 at 04:57
  • @arieljuod No errors or warnings in the console and the network tab just shows a 200 with the content within the HTTP response. The file that it is sending back also has an extension too with no luck. – LewlSauce Oct 06 '19 at 05:34
  • Are you using Turbolink? If yes, can you try to disable it as mentioned in https://stackoverflow.com/a/25826223/2245697 – Tan Nguyen Oct 06 '19 at 06:06

0 Answers0