如何在客户端将CSV导出到浏览器

时间:2016-10-02 17:37:56

标签: ruby-on-rails ruby csv ruby-on-rails-4 sendfile

当我从localhost向我的项目文件夹发出请求时,我可以导出CSV,但我无法弄清楚如何从localhost将其导出到我的浏览器下载文件夹。我正在使用send_data方法,如下所示。有什么想法吗?

class FitbitApiController < ApplicationController

  before_filter :authenticate_user!

  def data_request
  client = current_user.fitbit_client
  output_filename = ''
  case params[:resource]
  when 'daily_activity_summary'; output = client.activity_time_series(resource: 'calories', start_date: params[:date], period: '1d')
    output_filename = 'daily_activity_summary.csv'
  when 'sleep'; output = client.sleep_logs(params[:date])
    output_filename = 'sleep.csv'
  when 'activities/steps'; output = client.activity_time_series(resource: 'steps', start_date: params[:date], period: '1d')
    output_filename = 'steps.csv'
  when 'weight'; output = client.weight_logs(start_date: params[:date])
    output_filename = 'weight.csv'
end


  # Determine filename based on value of params[:resource]
  filename = params[:resource].split('/').last

  # Write data to CSV
  require 'json_converter'
  converter = JsonConverter.new

  converter.write_to_csv(output, output_filename, headers = true, nil_substitute = '')

  send_data output_filename, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment;output=#{output}.csv"

  end
end

0 个答案:

没有答案