Rails how to display a file with the correct file name

This is difficult to explain, so I will try my best and hopefully edit the question if people need more information. I do not provide the exact code, but just an example of a problem.

I am using rails 2.3.8. I am on Unix.

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

I have the following, in my opinion.

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

My controller has the following.

 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 file name is "myaction" instead of the file name.

Thoughts on how I can properly name him?

+4
source share
2 answers

Sounds like work for send_file . The x_sendfile option prevents your employees from being busy while transferring the actual file. You can learn more about this in this blogpost .

 send_file path_to_file_on_filesystem, :type => "application/zip", :x_sendfile => true 
+8
source

You want to use send_data with the :filename option. See the API documentation .

You want to be extremely careful with this. Never trust a client / user! They will send file=../../../../etc/group or something in this case to read arbitrary files on your system, so be sure that this is necessary before passing it to any reading methods files.

+8
source

Source: https://habr.com/ru/post/1334976/


All Articles