send_data:
————
send_data, Sends the given data to the browser. This method allows you to specify if the browser should display the response as a file attachment i.e. in a download or as inline data i.e; showing the output on the browser. For the send_data we may set the content type, the file name etc,

The options of a send_data are:

:filename – specifying a filename for the use on browser.
:type – specifies an HTTP content type. Defaults to ‘application/octet-stream’. we can specify either pdf/excel.
:disposition – specifies that the file shown inline or downloaded. Valid values are ‘inline’ and ‘attachment’ .Here attachment is the default dispostion.
:status – specifies the status code to send with the response. Defaults to ‘200 OK’.

eg:
send_data( out_data, :type => “text/xml”, :filename => “sample.xml”,:dispostion=>’inline’,:status=>’200 OK’ )

send_file:
———–
send_file ,Sends the file, by default it streams 4096 bytes at a time. This way the whole file doesn‘t need to be read into memory at once. This makes feasible to send even large files. You can optionally turn off streaming and send the whole file at once.
send_file(params[:path]) allows a malicious user to download any file on your server so need to be careful.

:filename – specifying a filename for the use on browser.
:type – specifies an HTTP content type. Defaults to ‘application/octet-stream’. we can specify either pdf/xml.
:length – used to manually override the length in bytes of the content whic is sent to the client. Defaults to File.size(path).
:disposition – sspecifies that the file shown inline or downloaded. Valid values are ‘inline’ and ‘attachment’ .Here attachment is the default dispostion.
:buffer_size – specifies size which in bytes of the buffer which is used to stream the file. Defaults to 4096.
:status – specifies the status code to send with the response. Default is ‘200 OK’.
:stream – whether to send the file to the user as it is read (true) or to read the entire file before send (false). Defaults to true.

send_file( out_data, :type => “text/xml”, :filename => “sample.xml”,:dispostion=>’inline’,:status=>’200 OK’,:stream=>’true’ )

send_data vs send_file in Rails

Leave a Reply

Your email address will not be published.