Download Blob file in Rails App is shown inline and not attached - ruby-on-rails

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..

Related

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

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.

Getting ActionController::MissingFile when trying to force a download (vice open) with send_file

I am using rails 4.x with paperclip. I want to ensure that when the user clicks on a link to download a paperclip attachment, the file is downloaded instead of opening up.
The link I was using that would open or save depending on the browser configuration was
<%= link_to image_tag("save.gif", document.doc_file.url, :target => "_blank" %>
That link will sometimes open the file instead of downloading.
I set up a method of
helper_method :download
def download
#document= Document.find(39)
send_file ("http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf?1458068410"),
:filename => #document.doc_file_file_name,
:type => #document.doc_file_content_type,
:disposition => 'attachment'
end
I hardwired the url for testing. I had also tried with send_file ("http://localhost:3000#{#document.doc_file.url}") and send_file (#document.doc_file.url). NEither worked.
My link to that is
<%= link_to image_tag("save.gif"), download_path(document.id) %>
routes.rb has
match 'documents/download/:id' => 'documents#download',via: [:get, :post], :as => 'download'
When I click on the download link, I get an error of
ActionController::MissingFile in DocumentsController#download
Cannot read file http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf
Rails.root: C:/Users/cmendla/RubymineProjects/technical_library
Application Trace | Framework Trace | Full Trace
app/controllers/documents_controller.rb:17:in `download'
If I put the URL into an address bar on a browser, it works. i.e. `http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf'
The send_file method accepts the physical location of the file on your server, not public URL. So something like the following should work, depending on where the PDF file actually resides:
send_file "#{Rails.root}/public/images/39/Medical_Opportunity_EvaluationForm.pdf",
:filename => #document.doc_file_file_name,
:type => #document.doc_file_content_type,
:disposition => 'attachment'
If this is a paperclip document, the path method should work:
send_file #document.doc_file.path,
:filename => #document.doc_file_file_name,
:type => #document.doc_file_content_type,
:disposition => 'attachment'
See the docs for more info.

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 download file with send_file?

Can someone enlighten me how can I download file with send_file?
I have a file image.jpg inside app/assets/images. I've tried this in my controller:
def download
send_file ("#{Rails.root}/public/images/image.jpg")
end
def download
send_file ("#{Rails.root}/assets/images/image.jpg")
end
def download
send_file ("#{Rails.root}/images/image.jpg")
end
def download
send_file ("/public/images/image.jpg")
end
def download
send_file ("/assets/public/images/image.jpg")
end
def download
send_file ("/assets/images/image.jpg")
end
For each path it says:
ActionController::MissingFile in HomeController#download
Cannot read file 'some_path'
What could be a problem here? Thanks!
Try:
IMAGES_PATH = File.join(Rails.root, "public", "images")
def download
send_file(File.join(IMAGES_PATH, "image.jpg"))
end
In your view =>
<%= link_to "click here to download", signed_feeds_pdf_path(:feed_image_path => feed_image.feedimage.path), target: '_self' %>
In your controller =>
def pdf
file_name = params[:feed_image_path].split('/').last
#filename ="#{Rails.root}/public/uploads/feed_image/feedimage/#{file_name}"
send_file(#filename ,
:type => 'application/pdf/docx/html/htm/doc',
:disposition => 'attachment')
end
soo simple......
well, i suggest you to move your file to public folder. Anyway , do this
send_file(Rails.root.join('app' , 'assets', 'images', 'image.jpg'))
We need to specify the mine type so that it will cache.
send_file ("#{Rails.root}/public"+image.image_path), :type => "image/jpeg", :disposition => 'inline'
For anyone still looking for an answer, send_data and send_file won't work when responding to ajax calls. Instead, try submitting a form or using <a href=..> to call the controller method and download a file.

Ruby on rails file download link

I created file upload using paperclip! File uploads as it is supposed to.
Than I added file download method like this:
def download
sample = Sample.find(params[:id])
send_file upload.sample.path,
:filename => upload.sample_file_name,
:type => upload.sample_content_type,
:disposition => 'attachment'
flash[:notice] = "Your file has been downloaded"
end
But I cant figure out, what should I put in my show action so that I would be able to download the file?
I got as far as this:
<td><%= #sample.upload_file_name =%></td>
<%= link_to 'Download', :action => :download, :path =>#sample.upload.url, :type => #sample.upload_content_type %>
But it shows error : Couldn't find Sample with id=download
Can anyone help me?
Think you need to change your link_to to send the :id (that's what the controller action is looking for with params[:id])
<%= link_to 'Download', :action => :download, :id => #sample.id %>
Soz Karlis was writing as you posted :D
=link_to 'download', #sample.upload.path
This is the easiest way))
Assuming you set download named route properly, you can just say
<%= link_to 'Download', download_sample_path(#sample) %>
It looks like you have mismatch in your routes.
That is why, you have to:
Add additional route to your routes.rb for download action.
As it was said previously, you have to add link to your view (link_to).
Read this post for more advanced Download functionality:
http://thewebfellas.com/blog/2009/8/29/protecting-your-paperclip-downloads

Resources