rails how to render a file with correct filename - ruby-on-rails

This is tough one to explain so i'll try my best, and hopefully edit the question if people need more information. I am not providing exact code, but merely an example of the issue.
I am using rails 2.3.8. I am on Unix.
I have a bunch of files under a directory not Apache accessible. (i.e. /data/files/file.rpk)
I have the following in my view.
link_to "RPK File", :controller => 'mycontroller', :action=> 'myaction', :file => '/data/files/file.rpk'
I have the following in my controller.
def myaction
if FileTest.exists?(params[:file])
render :file => params[:file]
end
end
When i select the link on the page i get a download prompt for my desired file, but the name of the file is "myaction" instead of the filename.
Thoughts on how i could get it named correctly?

Sounds like a job for send_file. The x_sendfile option prevents that your workers keep busy while transferring the actual file. You can read more about that in this blogpost.
send_file path_to_file_on_filesystem, :type => "application/zip", :x_sendfile => true

You want to use send_data with the :filename option. See the API documentation.
You want to be extremely careful with this, though. Never ever trust the client/user! They will send file=../../../../etc/group or something in order to read arbitrary files on your system, so be very sure to sanitize that value before passing it to any file-reading methods.

Related

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?

Paperclip Force Download

Hope anyone can help on this!
Making use of Paperclip to upload Files to an Application, so two things that i need to cover.
1) What is the best way to link to the File for Download, I am currently linking to the public folder but it is not the best option as it shows the URL which i dont want. Thinking maybe a button_to call be not sure if this is build into Paperclip already. Options.
2) When above is done then the Browser needs to force to download not just open the file, or at least give the user the standard firefox option open or save.
Please Assist Thanks a Mil!!!
I'll answer question NÂș2.
Let's say your model is called gallery.
In your gallery controller you'll add a download method:
def download
#gallery= Gallery.find(params[:gallery_id])
send_file #gallery.gallery_picture.path,
:filename => #gallery.gallery_picture_file_name,
:type => #gallery.gallery_picture_content_type,
:disposition => 'attachment'
end
Now from your routes you'll invoke the root to this method:
match 'gallery/:id' => 'gallery#download', :as => :download
In your views:
- #galleries.each do |gallery|
= link_to 'Download Picture', download_path(gallery.id)
I'm away from home and I can not test this code, but you can visit those questions I did with the same question as yours. Let me know if it solves your problem.
Rails send_file multiple styles from Paperclip, how can I avoid code repetition?
Rails 3.0 associated model with download 'save to' image
In rails, send_file with a parameter set by user is a security hole. More information
In rails 4 you can't just write match, you have to add at the end , via: [:get, :post]. More information

Rails simple file download

hi help me in simple question:
how to released simple download:
i my public/data i have some.txt or some.pdf file ( with some text )
and i want to user click in some button and start download this file.
i do something like that
<%= link_to "Terms" ,:action => :download, :file_name => 'some.txt' %>
def download
send_file "#{RAILS_ROOT}/public/data/#{params[:file_name]}", :type=>"application/zip"
end
But what to do next?
PS if you have some tutorial or example on this subject (like downloading file), I would be very grateful
You are definitely missing a route in your routes.rb:
resources :posts do
get :download, :on => :collection
resource :comments
end
and then you can have a link like:
<%= link_to "Terms" ,:action => :download, :file_name => 'some.txt' %>
that will probably generate something like:
Terms
I think it's still not the best solution, but solves your problem.
I recommend a quick look at this part of the Rails Guides to clarify this:
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
A complete read of this guides is always good and highly recommended too.
What exactly do you mean "what next"? Apart from the fact that the application/zip mime type doesn't match your file's (which is .txt, so presumable text/plain) what you wrote is essentially correct.
Two things to keep in mind, since your file is in the public directory you don't strictly need to use send_file through the controller, you could just write this instead:
<%= link_to "Terms", '/data/some.txt' %>
And second if you're going to use send_file to server larger files you might want to look in to xsendfile instead which improves performance. This has to be supported by your webserver though (using mod_sendfile for apache for example.)
But all that said your code should work as is.

Saving XML files with Rails

im working on a Rails project that should create XMl files, or to be more specific
use existing XMl templates and put content from the database in it.
So i dont need to create the xml structure, basically just rendering a template with content.
What would be the smartest way to do that?
So far i have a file.xml.erb in my layout folder
and i have a custom route "/renderXML" that does
def renderXML
#reading_question = ReadingQuestion.find(params[:id])
render :file => 'layouts/question.xml'
end
This works, but i also want to save the file, not only show it (actually viewing it is not really needed).
For saving i found this
File.open('fixed.xml','w'){|f| f.write builder.to_xml}
How do i access the rendered file and save it with some method like above?
Perhaps something like:
s = render_to_string :file => 'layouts/question.xml'
File.open('fixed.xml','w'){|f| f.write s}
render :text => s
Another approach :
send_data fixed, :type => 'text/xml; charset=UTF-8;', :disposition =>
"attachment; filename=fixed.xml"

Problems with Prawnto options

I'm using Prawnto to generate PDFs in my Rails app. I want three specific options set for my PDFs:
I don't want it to start with a blank page
I want it to download directly (not inline)
I want to specify the filename
Here's my controller method:
def print
#purchase = Purchase.find(params[:id])
prawnto :prawn=>{:skip_page_creation=>true}, :inline=>false, :filename=>#purchase.deal.name + "-" + #purchase.customer.name+".pdf"
end
Without the :skip_page_creation option, the other two options (inline and filename) work fine. But when I add the skip_page_creation option, it goes inline with a default filename. And of course, if I remove skip_page_creation, I get a nice downloaded PDF with a first blank page.
The docs for this library leave something to be desired, but can anyone point me in the right direction?
Cheers!
Aaron.
I've just tried this by changing one of my inline examples which worked ok:
module SharedPdfs
def show
prawnto :prawn => {:skip_page_creation=>true}, :inline => false, :filename => "results_pdf.pdf"
render :template => '/results/show'
end
end
Had a quick look at the prawnto source and it should pickup your prawn options not sure why it isn't but at least you've got it working for now.

Resources