send_data
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0 (0)
- 3.2.1 (38)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-2)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (-2)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (6)
- 7.1.3.4 (0)
- What's this?
send_data(data, options = {})
private
Sends the given binary data to the browser. This method is similar to render plain: data, but also allows you to specify whether the browser should display the response as a file attachment (i.e. in a download dialog) or as inline data. You may also set the content type, the file name, and other things.
-
:filename - suggests a filename for the browser to use.
-
:type - specifies an HTTP content type. Defaults to application/octet-stream. You can specify either a string or a symbol for a registered type with Mime::Type.register, for example :json. If omitted, type will be inferred from the file extension specified in :filename. If no content type is registered for the extension, the default type application/octet-stream will be used.
-
:disposition - specifies whether the file will be shown inline or downloaded. Valid values are "inline" and "attachment" (default).
-
:status - specifies the status code to send with the response. Defaults to 200.
Generic data download:
send_data buffer
Download a dynamically-generated tarball:
send_data generate_tgz('dir'), filename: 'dir.tgz'
Display an image Active Record in the browser:
send_data image.data, type: image.content_type, disposition: 'inline'
See send_file for more information on HTTP Content-* headers and caching.
Example to auto download
Controller
op = Operation.find(params[:id]) fname = "operation_#{op.id}_#{DateTime.now.to_i}.csv" send_data op.export(params[:url_type]), :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=#{fname}.csv"
export_csv
def export(url_type) csv_data = CSV.generate do |csv| csv << self.header_columns # simple array ["id","name"] url_items = @operation.url_items.where(:url_type => url_type) url_items.each do |url_item| csv << self.process_row(url_item) # simple array [1,"bob"] end end return csv_data end
Send with filename
The Content-Disposition response header holds the suggested attachment filename (i.e. “attachment; filename=fname.ext”)
Set the :disposition option to pass this name.
Controller
http = Net::HTTP.new(@page.host) res = http.post(path, info.to_query, headers) send_data res, :content_type => res.content_type, :disposition => res["Content-Disposition"], status: res.code