开发者

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 dir开发者_JAVA百科ectory 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?


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


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

You want to be extremely careful with this, though. Never ever trust the client/user! They will send file=../../../../etc/group or something in order to read arbitrary files on your system, so be very sure to sanitize that value before passing it to any file-reading methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜