当使用send_data发送excel文件时,Rails显示呈现的文本模板

时间:2014-07-30 19:51:12

标签: ruby-on-rails ruby

我使用表单将数据发布到rails控制器。发送的数据是excel文件的二进制数据,控制器应将其回送给浏览器以触发下载。 (我必须这样做)。

问题是我在Chrome中收到警告:

Resource interpreted as Document but transferred with MIME type      application/vnd.openxmlformats-officedocument.spreadsheetml.sheet: "http://192.168.2.39:3000/resources/export"

我认为奇怪的是Rails显示"呈现文本模板"在请求结束时。

以下是控制器操作:

def export
    begin
        cookies.delete(:download_token)
        cookies[:download_token] = params[:download_token] 
        send_data  Base64.decode64(params[:data]), :filename => 'Tasks.xls', :type =>   'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8; header=present', :disposition => 'attachment'
    rescue Exception => e
        render nothing: true
    end
end

这是导出操作输出的结束:

"application/x-www-form-urlencoded"
Rendered text template (0.0ms)
Sent data Tasks.xls (1.2ms)
Completed 200 OK in 5006ms (Views: 0.9ms | ActiveRecord: 0.0ms)

我正在使用 Rails 4 该请求来自于在EXT js 4 app中定位iframe的表单 整个应用程序有一些html视图和一些使用ext js的视图 我正在使用turbolinks gem来查看html视图。

实际发送文件,浏览器触发下载。它只是我想要摆脱的警告。

更新

我禁用了turbolinks,警告仍在那里。

1 个答案:

答案 0 :(得分:1)

我在Chrome上找到了针对该警告的修复程序。显然,Safari上的错误是每次下载时都会出现的错误,而不是特定于我的应用程序。

在config / initializers / mime_types.rb中:

Mime::Type.register "vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xls

在我的控制器中:

send_data  Base64.decode64(params[:data]), :filename => 'Tasks.xls', :type => :xls, :disposition => 'attachment', :layout => false

这使得Chrome上的警告消失了。希望它有所帮助。