Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in rails, unable to override content-disposition filename

I am using Rails 3.1.0.rc8 and Chromium 15.0.874.102.

I want to set the filename of a CSV download. I am following this SO solution, but find myself unable to modify the filename of 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 log:

[2011-11-28 12:25:49.611] DEBUG - HEADERS: {"Content-Type"=>"text/csv", "Content-Disposition"=>"attachment; filename=my_filename.csv"}

In Chromium network inspector tool, I see the following in my 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 the change come through in my network inspector tool. No matter what I set the filename to, it remains index.csv.

Thanks, Max

like image 455
maxenglander Avatar asked Nov 28 '11 17:11

maxenglander


2 Answers

According to the Content-Disposition section of the appendices to the HTTP/1.1 specification the filename must be a quoted string:

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

An example is

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

So you probably need to make the following change (note the quotation marks):

headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
like image 188
maerics Avatar answered Sep 23 '22 09:09

maerics


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

like image 28
maxenglander Avatar answered Sep 21 '22 09:09

maxenglander