Differencebetween Send_Data and Send_File in Ruby on Rails

What is the difference between send_data and send_file in Ruby on Rails?

send_data(_data_, options = {})
send_file(_path_, options = {})

Main difference here is that you pass DATA (binary code or whatever) with send_data or file PATH with send_file.

So you can generate some data and send it as an inline text or as an attachment without generating file on your server via send_data. Or you can send ready file with send_file

data = "Hello World!"
send_data( data, :filename => "my_file.txt" )

Or

data = "Hello World!"
file = "my_file.txt"
File.open(file, "w"){ |f| f << data }
send_file( file )

For perfomance it is better to generate file once and then send it as many times as you want. So send_file will fit better.

For streaming, as far as I understand, both of this methods use the same bunch of options and settings, so you can use X-Send or whatever.

UPD

send_data and save file:

data = "Hello World!"
file = "my_file.txt"
File.open(file, "w"){ |f| f << data }
send_data( data )

rails send_file and send_data sends out zero byte files

Try sending a simple file to see if it works

send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'

Read this thread, I think it has everything you need.

Rails send multiple files to browser using send_data or send_file

So the issue i was having is that send_file does not respond to an ajax call which is how i was posting to that action. I got rid of the ajax, and am sending the necessary data though a hidden_field_tag and submitting it with jquery. The code below creates files for the data, zips them, and passes the zip file to send_data.

file_data.each do |hash|
hash.each do |key, value|
if key == :json_encrypted
@data = value
elsif key == :filename
@file_name = value
end
end

name = "#{Rails.root}/export_multiple/#{@file_name}"
File.open(name, "w+") {|f| f.write(@data)}

`zip -r export_selected "export_multiple/#{@file_name}"`
send_file "#{Rails.root}/export_selected.zip", type: "application/zip", disposition: 'attachment'
end

Ruby On Rails: Sending large pictures using send_data/send_file results in errors

I think I found the answer.
I replaced WEBrick by Thin as you can see on this website.
It seems that WEBrick has problems with larger files. I don't know if Thin is the final solution, but at least it solved the problem.

Is it possible to use send_data (not send_file) via ajax request in rails?

This works. use button_to

Example:

<%= button_to(<custom_action_sending_pdf_path>, method: :get, class: 'btn btn-primary') do %>
<i class="fa fa-print" aria-hidden="true"></i></i> Print PDF
<% end %>

Let that button go to that custom action which downloads the pdf (same code as in original question):

class MyController < ApplicationController
def custom_action_sending_pdf
pdf = InitMyPdf.new(@user)
send_data pdf.render, filename: "complete_report.pdf", type: "application/pdf"
end
end

What happens now is when that button is clicked:

  • The pdf downloads and pops up adobe (at least it does this in safari)
  • The url of the page does not change to the custom action which prints the pdf. So for the next request: request.referrer will not be that custom action for the pdf, which is the desired behavior here.

send_file/send_data not sending file for downloading

I dont know why this method is working. But serves my purpose.

$("#btn_id").on('click', function () {
window.location = ypur api url;
});

Thanks,

Ajith

Rails send_file/send_data - Cannot Read File - After web service call

I found the solution. send_file is the correct stream mechanism to use but I needed to decode the string while writing to the file. I also need to add the 'b' parameter to the File.open call.

This works:

File.open(@report_name, "wb+") do |f|
f.puts Base64.decode64(@report)
end



@file = File.open(@report_name, 'r')

send_file @file, :filename => @report_name, :type => "application/pdf; charset=utf-8", :disposition => "attachment"

How to solve the memory-leak with send_file (or send_data) on Heroku?

The answer is to start garbage collection with:

GC.start

I placed that line at the bottom of the Rails controller action after send_data.

http://www.ruby-doc.org/core-1.9.3/GC.html

rails media file stream accept byte range request through send_data or send_file method

I've been able to serve up the files with some success using send_file. Although I have one hitch, seeking to an earlier part of the song causes a new request which makes the song restart from 0:00 instead of the true location from the seekbar. This is what I have working for me so far:

  file_begin = 0
file_size = @media.file_file_size
file_end = file_size - 1

if !request.headers["Range"]
status_code = "200 OK"
else
status_code = "206 Partial Content"
match = request.headers['range'].match(/bytes=(\d+)-(\d*)/)
if match
file_begin = match[1]
file_end = match[1] if match[2] && !match[2].empty?
end
response.header["Content-Range"] = "bytes " + file_begin.to_s + "-" + file_end.to_s + "/" + file_size.to_s
end
response.header["Content-Length"] = (file_end.to_i - file_begin.to_i + 1).to_s
response.header["Last-Modified"] = @media.file_updated_at.to_s

response.header["Cache-Control"] = "public, must-revalidate, max-age=0"
response.header["Pragma"] = "no-cache"
response.header["Accept-Ranges"]= "bytes"
response.header["Content-Transfer-Encoding"] = "binary"
send_file(DataAccess.getUserMusicDirectory(current_user.public_token) + @media.sub_path,
:filename => @media.file_file_name,
:type => @media.file_content_type,
:disposition => "inline",
:status => status_code,
:stream => 'true',
:buffer_size => 4096)


Related Topics



Leave a reply



Submit