Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails how to render a file with correct filename

This is tough one to explain so i'll try my best, and hopefully edit the question if people need more information. I am not providing exact code, but merely an example of the issue.

I am using rails 2.3.8. I am on Unix.

I have a bunch of files under a directory not Apache accessible. (i.e. /data/files/file.rpk)

I have the following in my view.

link_to "RPK File", :controller => 'mycontroller', :action=> 'myaction', :file => '/data/files/file.rpk' 

I have the following in my controller.

def myaction
  if FileTest.exists?(params[:file])
    render :file => params[:file]  
  end
end

When i select the link on the page i get a download prompt for my desired file, but the name of the file is "myaction" instead of the filename.

Thoughts on how i could get it named correctly?

like image 400
Kirby Avatar asked Dec 06 '22 00:12

Kirby


1 Answers

Sounds like a job for send_file. The x_sendfile option prevents that your workers keep busy while transferring the actual file. You can read more about that in this blogpost.

send_file path_to_file_on_filesystem, :type => "application/zip", :x_sendfile => true
like image 140
Bjorn Avatar answered Dec 18 '22 22:12

Bjorn