CSV.open and send_data in rails....? - ruby-on-rails

I may end up figuring this out later, but i thought I'd try.
Can someone help combine send_data and CSV.open
According to the docs, you can CSV.open filename, mode(whatever that is) and basically a file will save to your current path. However, if you want to send that file to a user through his broswer, as most of us who give the option of CSV files to download, do,do... then can we combine the CSV.open with send_data?
thoughts?
examples welcome if you do something like this too.

I don't think you want to combine those two things.
CSV.open will save the data to a file, which you would need to read back in in order to send it through send_data.
But you can do something like:
csv = []
csv << ["titles", "for", "csv"]
csv << ["data", "for", "csv"]
send_data(csv.collect{|s| s.join(",")}.join("\n"),
:type => 'text/csv; charset=utf-8; header=present',
:filename => "mytitle.csv")
Which should prompt the user to download the csv file.

Related

Invalid/damaged download using send_data with PowerPoint

I am generating a PowerPoint using the powerpoint gem and up until now I was using send_file, but now I want to use send_data. The reason for this is that send_data blocks the application from doing anything else until the call completes, while send_file allows the app to continue executing while the file downloads, which sometimes, due to coincidental timing, leads to the file being deleted before the user tells their browser to start the download and the web server finishes sending the file and thus a blank or incomplete file is downloaded.
This is how I create my PowerPoint:
#Creating the PowerPoint
#deck = Powerpoint::Presentation.new
# Creating an introduction slide:
title = 'PowerPoint'
subtitle = "created by #{username}"
#deck.add_intro title, subtitle
blah blah logic to generate slides
# Saving the pptx file.
pptname = "#{username}_powerpoint.pptx"
#deck.save(Rails.root.join('tmp',pptname))
Now, on to what I have tried. My first instinct was to call as follows:
File.open(Rails.root.join('tmp',"#{pptname}"), 'r') do |f|
send_data f.read, :filename => filename, :type => "application/vnd.ms-powerpoint", :disposition => "attachment"
end
I've also tried sending the #deck directly:
send_data #deck, :filename => pptname, :disposition => "attachment", :type => "application/vnd.ms-powerpoint"
Reading it ends with a larger file than expected and it isn't even able to be opened and sending the #deck results in a PowerPoint with one slide that simply has the title: #Powerpoint::Presentation:0x64f0dd0 (aka the powerpoint object).
Not sure how to get the object or file into binary format that send_data is looking for.
You need to open the file as binary:
path = #deck.save(Rails.root.join('tmp',pptname))
File.open(path, 'r', binmode: true) do |f|
send_data f.read, filename: filename,
type: "application/vnd.ms-powerpoint",
disposition: "attachment"
end
This can also be done by calling the #binmode method on instances of the file class.
send_data #deck, ...
Will not work since it calls to_s on the object. Since Powerpoint::Presentation does not implement a #to_s method you're getting the default Object#to_s.

[Ruby]IO.Read not reading the whole file

I want to download a zip file containing some images via a rails method. The problem is that when I try to read thoses files, I'm not getting the entire file. Of course, that's pretty much annoying to display the image.
Here is my code :
def download_zip(image_list)
if !image_list.blank?
file_name = "pictures.zip"
t = Tempfile.new("lolbite11")
Zip::OutputStream.open(t.path) do |z|
image_list.each do |img|
title = img.name
title += ".jpg" unless title.end_with?(".jpg")
z.put_next_entry(title)
z.print IO.read(Rails.root.join('public', img.path)) <- Here is the problem ?
end
end
send_file t.path, :type => 'application/zip',
:disposition => 'attachment',
:filename => file_name
t.close
end
end
After I've download the zip and extract it. I open my images with a text editor and sadly see that I've not the whole image... I tried to output the 'IO.read' and it's only displaying what is in my final file.
I finally found an other way to solve my problem with File.copy_stream().
But if someone knows why this doesn't work, feel free to explain to me.

Rails download http response/displayed site

Instead of displaying the xml file rendered by the index.api.rsb file in my browser, i want to download it. To me this sounds very simple, but I cant find a solution.
I tried the following in the controller-method:
def split
if params[:export] == "yes"
send_file *here comes the path to xml view*, :filename => "filename", :type => :xml
end
respond_to ...
end
The result is a MissingFile exception...
Thanks in advance
Note that :disposition for send_file defaults to 'attachment', so that shouldn't be a problem.
If you have a MissingFile exception, that means the path is incorrect. send_file expects the path to an actual file, not a view that needs to be rendered.
For your case, render_to_string might be what you need. Refer to this related question. It renders the view and returns a string instead of setting the response body.
def split
if params[:export] == "yes"
send_data(render_to_string path_to_view, filename: "object.xml", type: :xml)
end
end
To force it to download it, add :disposition => attachment to your send_file method.
Source: Force a link to download an MP3 rather than play it?

download csv file to computer

From ajax I call my export action in my controller
def export
data_filtered = eval(params[:data_filtered][9..-2])
CSV.open("data.csv", "wb") do |csv|
csv << data_filtered.first.keys
data_filtered.each do |hash|
csv << hash.values
end
end
end
Currently this does indeed create a file called data.csv on my computer but it doesn't show any evidence of actually doing so. I want the file to be downloaded by the browser and show up in the browser downloads and also in my downloads folder.
EDIT:
def export
data_filtered = eval(params[:data_filtered][9..-2])
csv_file = CSV.generate({}) do |csv|
csv << data_filtered.first.keys
data_filtered.each do |hash|
csv << hash.values
end
end
send_data csv_file, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=tester1.csv"
end
You need to set headers to indicate what the browser should be doing with the resulting file.
send_data file, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment;data=#{csv_file}.csv"
However, it would have to be written first, to the server, and then downloaded. You might find this answer helpful: Ruby 1.9.2 export a CSV string without generating a file
Well it appears you are not sending back any response from your controller method, which is why nothing is happening in the browser. You are just processing the CSV.
To send back a downloadable entity a response, one way would be to use either send_data or send_file, depending on what format you have the data in (binary vs. a file).
Both essentially do the same -- allow you to specify the data, the data type, and a downloadable file name that you specify. It can be returned as a response, just like render calls.
Hope that helps!

pdftk, tempfile and rails

Ok, I've tried all kinds of stuff and I'm not entirely sure this will work. The pdfs I need to merge are on the server and the links to them are hardcoded. pdftk works locally in my terminal, but not with these remote links. So I'm unsure if this will work once I get it up to production.
Basically, I'm trying to write a method that will retrieve a selected group of pdfs and merge them into one pdf for the user to download.
But I'm having a hard time deciphering how to utilize tempfiles and running terminal commands through the rails app.
Here is the method:
def create
#routes = TransitRoute.find(params[:selected_routes])
#selected_routes = #routes.collect {|x| x.new_pdf_link + " "}
Tempfile.open('prefix', "#{Rails.root}/tmp") do |f|
f.print("pdftk #{#selected_routes} cat output temporary.pdf")
f.flush
f.read
end
respond_to do |format|
format.html
end
end
I have a couple questions:
My tempfile has the correct command line written to it:
pdftk 1.pdf 2.pdf cat output new.pdf
How do I get this line run so that the new.pdf is created?
Am I supposed to replace the tempfile with the new pdf, or write the new pdf to it or just make the new pdf in it's own location? If the latter, how do I get it to be temporary?
How do I get a link to the new pdf so users can download it?
Some of this may be basic stuff, but I've never had to mess with tempfiles of making pdfs dynamically like this.
Oh yeah, and this app is also in Rails 2.3
Thanks for any help.
Ok, I have it working. Here's the new code incase someone has advice for improvement or has the same question:
def create
file = Tempfile.new('temp_route_pdf', "#{Rails.root}/tmp/")
#routes = TransitRoute.find(params[:selected_routes])
selected_routes = #routes.collect {|x| x.new_pdf_link + " "}
route_names = #routes.collect {|x| x.short_name + "_"}
#generated_pdf_file = "#{route_names}routes.pdf"
`pdftk #{selected_routes}cat output #{file.path}`
raise Exception unless $?.success?
send_file( "#{file.path}",
:type => "application/pdf",
:filename => "#{#generated_pdf_file}",
:disposition => 'inline')
end

Resources