I am using Rails 3.1.0.rc8 and Chromium 15.0.874.102.
I want to set the CSV download file name. I follow this SO solution but cannot change the filename the Content-Disposition header.
Here is my code:
module ActionController module CSVHelper def render_csv options={} if request.env['HTTP_USER_AGENT'] =~ /msie/i headers['Pragma'] = "public" headers['Content-Type'] = "text/plain" headers['Cache-Control'] = "no-cache, must-revalidate, post-check=0, pre-check=0" headers['Expires'] = "0" else headers['Content-Type'] = "text/csv" end filename = generate_filename options.delete(:basename) headers['Content-Disposition'] = "attachment; filename=#{filename}" end def generate_filename basename=nil, suffix="csv" filename = basename || params[:action] filename << ".#{suffix}" filename end end end
And in my controller:
respond_to do |format| format.html format.csv do render_csv(:basename => "my_filename") Rails.logger.debug "HEADERS: #{headers.to_s}" end end
In my journal:
[2011-11-28 12:25:49.611] DEBUG - HEADERS: {"Content-Type"=>"text/csv", "Content-Disposition"=>"attachment; filename=my_filename.csv"}
In the Chromium Network Inspector tool, I see the following in the response headers:
Content-Type: text/plain Content-Disposition: attachment; filename=index.csv
If I change the Content-Type to something like foo/bar , I see that this change has occurred in my network inspector tool. No matter what I installed filename , it remains index.csv .
Thanks Max
maxenglander
source share