In rails, unable to override content file name

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

+7
source share
4 answers

According to the Content-Disposition section of the annexes to the HTTP / 1.1 specification, the file name must be a quoted string:

  filename-parm = "filename" "=" quoted-string ... 

An example is

  Content-Disposition: attachment; filename="fname.ext" 

Therefore, you will probably need to make the following changes (mark the quotation marks):

 headers['Content-Disposition'] = "attachment; filename=\"#{filename}\"" 
+11
source

I found out that my team uses the csv_builder gem, which allows you to adjust the name of the downloaded file by setting @filename in the controller. Will continue to award the correct answer to anyone who can explain why I get the behavior described in the OP.

+2
source

I do not see where you really send / visualize the output ... Is it in the .csv.erb file or something like that? Or use csv_builder as another answer? This could be a trick.

I came up with the same thing as .xlsx output. Once I had the file on disk, I could do this:

 def example respond_to do |format| format.xlsx do path = some_method_generating_xlsx_file headers['Content-Disposition'] = "attachment; filename=test.xlsx" render :text => File.binread(path), :content_type => XLSX_MIME_TYPE end end end 

in my controller and it worked fine. Perhaps the headers were knocked down using a template.

+1
source

I personally do not use render for such content - I prefer to use send_data . Here is an example:

 send_data data, :type => 'text/csv', :disposition => 'attachment; filename=my_file_name.csv' 
0
source

All Articles