Download a PDF file with ruby-on-rails, error message - ruby-on-rails

I followed the steps setting a link to download a PDF file on a preview question about that.
Problem is, when I click on the download button, it open a window to show the PDF, but the PDF doesn't show up...
When I open or download it, I got this message "File type unknown (application/octet-stream) is not supported"
What's wrong?
Thank you
Here, how i set it up:
Controller
class HomesController < ApplicationController
def index
end
def download_pdf
send_file(
"#{Rails.root}/app/assets/docs/dossier_de_presentation_lvsl.pdf",
type: "application/pdf"
x_sendfile: true
)
end
end
routes.rb
get 'download_pdf', to: "homes#download_pdf"
view
<%= link_to "Download Pdf", "/assets/dossier_de_presentation_lvsl.pdf", :class => "themed_text", class: "btn btn-lg btn-custom" %>

Your controller action is not used at all. Browser tries to download file from the following path public/assets/dossier_de_presentation_lvsl.pdf.
If you want to download it through the controller action. You should set your application in following way:
In your routes:
get '/download_pdf', to: 'homes#download_pdf', as: 'download_pdf'
In your controller:
def index
end
def download_pdf
send_file(
"#{Rails.root}/app/assets/docs/dossier_de_presentation_lvsl.pdf",
type: "application/pdf",
disposition: 'attachment', # 'inline' if you want to show PDF in the browser instead of downloading it directly
x_sendfile: true
)
end
end
In your view:
<%= link_to "Download Pdf", download_pdf_path(format: :pdf), :class => "themed_text", class: "btn btn-lg btn-custom" %>
Or if you want, you can put your PDF directly to public folder in your rails app eg. to public/pdf/dossier_de_presentation_lvsl.pdf
and than you can use just a link:
Download PDF
But you can use this only if your PDF can be downloaded by everyone without any authentication etc. On the other hand you would not have to bother with the controller action and routes, it will be handled for you automatically.

Related

Download and redirect in rails

I have to put a link to download pdf file on my Rails app and it works.
But after download I don't want users in .../download_pdf.fr page but I want to redirect them in index.
I tried to put redirect_to root_path in download method in my controller but download didn't work with that.
If you have any idea.
Thank you a lot
Routes
get 'download_pdf', to: "home#download_pdf"
Controller
def download_pdf
send_file "#{Rails.root}/app/assets/images/file.pdf", type: "application/pdf", x_sendfile: true
end
View
<%= link_to "Télécharger", download_pdf_path, class:"button1" %>

Download Blob file in Rails App is shown inline and not attached

I get a blob from a function in my oracle Datenbank. Users of my Rails App should be able to download these BLOB. My Problem is, that after clicking on download, the BLOB file is shown inline in the browser, but it should be download as a file.
link:
= link_to export_pldw_plausibility_path, :class => "btn btn-primary
action-button" do
%span.glyphicon.glyphicon-pencil
%span.hidden-xs
= "Export"
route:
resources :pldw_plausibilities do
get 'export', on: :member
end
controller:
def export
send_data PldwPlausibility.export(params[:id]), :filename => "test.BLOB",
:disposition => 'attachment'
end
Model:
def self.export(id)
return plsql.pld.pld_mdc.exportMDOC('PLD_PLAUSIBILITY', id.to_i)
end
Screenshot
Sorry!
Adding :target => "_blank" to the link solves this Problem..

Ruby on Rails send_file doesn't work until i refresh the page?

I am working on a Rails server which I can download my locally stored movies and anime etc from. This is kind of working but when I click the download link I have to refresh the page in order for the download to actually start.
This is the controller that handles the download:
class DownloadController < ApplicationController
def index
#title = params[:title]
#name = params[:name]
#path = '/media/sf_Anime,_VN,_LN/Watching, not watched yet/'+#title+'/'+#name
send_file( #path )
end
end
and this is the link that links to that controller:
<% #episodes.each do |x| %>
<p> <%= x %><%= link_to " Download",
{controller: 'download', action: 'index', title: #title, name: x } %> </p>
<% end %>
edit:
I did some testing today and noticed that the download links work instantly if i try to send a smaller file (text or image). I also noticed that the download links actually works for the movies aswell but it takes 20-30 seconds for the download to start.
Do you have any idea of what would cause this delay?
Are you using turbolinks? Turbolinks apparently doesn't play nicely with send_file (https://github.com/rails/turbolinks/issues/182). Try adding "data: { no-turbolink: true }" (or "'data-no-turbolink'=>true") in the link_to helper, e.g.:
<%= link_to "Downloadable File", downloadable_file, data: { no-turbolink: true } %>
See also: Rails 4, asset pipeline causes user downloadable files to be downloaded twice, rails won't send_data as file, Ruby on Rails send_file, code in controller action running twice
Edited to reflect comment below. I would simple add a concern for handling downloads and then use
include Concerns::Downloads
to handle your download request. the routes.rb would look like this.
resources :movies do
member do
post 'download'
end
and in the view
<%= link_to 'Download', {:controller => 'movies', :action => 'download'}, {:method => :post } %></a>
Move the file to public folder
add only file name into link_to
<%= link_to "Downloadable File", "/"+filename, %>
Try setting the disposition to attachment in send_file:
class DownloadController < ApplicationController
def index
...
send_file( #path, :disposition => 'attachment' )
end
end
The issue may be that your browser is trying to open the file itself - :disposition => 'attachment' prompts the browser to download the file, even if it thinks the file is something that it can open.

How to create a download button (link) for a local image (png)

In my Ruby on Rails 4 application I want to provide the user with a download for a png image.
Firstly, where would this png need to be placed:
/public
/assets/images
Secondly, how would I do that?
I've tried what the 2nd answer here says, and I am getting this error:
No route matches [GET] "/public/diagram.png"
The implementation of the above answer:
At my view:
<%= link_to "DOWNLOAD", "/public/diagram.png" %>
The controller:
class ControllerNamesController < ApplicationController
// other actions defined: index, show, create, new, edit, update, destroy
def download_png
send_file(
"#{Rails.root}/public/diagram.png",
filename: "diagram.png",
type: "application/png"
)
end
Τhe routes file (has all the controllers defined like this):
resources :ControllerName
get "ControllerName/download_png", as: :download
Try using this
view
<%= link_to "Download" ,:action => :download %>
controller
def download
send_file '/home/blog/downloads/away.png',:type=>"application/png", :x_sendfile=>true
end
do this in route.rb
get "home/download_png" , as: :download
in view, change this
<%= link_to "DOWNLOAD", download_path %>
For the question,putting the images in /public would be fine. And for the error which you are getting,this is the problem
You are just putting the path of the image file in the link_to helper while it expects a route.
Try changing it to
<%= link_to "DOWNLOAD", home_download_png_url %>
Edit
Can't think why it didn't worked.Okay,as #nithinJ suggested you can use
<%= link_to "DOWNLOAD", "/diagram.png" %>
And as you mentioned,you want it to be downloded rather than opening in the new brower,you could do this in the controller
send_file '#{Rails.root}/public/diagram.png', type: 'image/png', disposition: 'attachment'
For more info,see send_file.

Routing Error with Ruby on Rails

I'm fairly new to Ruby on Rails and I'm having what seems like an easy problem but I can't seem to figure out what I have done wrong. My homepage has a button and when you click the button, its supposed to create an xml file with information from the database.
Button code:
<%= button_to "Create Google File", :action => :create_google_file %>
Controller code:
class ProductsController < ApplicationController
def new
end
def create_google_file
#products = Product.find(:all)
file = File.new('dir.xml','w')
doc = #products.to_xml
file.puts doc
file.close
end
end
The error I'm getting is
No route matches {:action=>"create_google_file", :controller=>"products"}
Add this to your config/routes.rb file
match "/create_google_file" => "products#create_google_file", :as => :create_google_file
And change the button to this
<%= button_to "Create Google File", create_google_file_path %>

Resources