Send_file open file in browse instead of download it - ruby-on-rails

Please tell me how this works, because I don't understand... Here is my simple code:
Route:
get 'download' =>'pages#download'
My action:
def download
send_file "#{Rails.root}/public/downloads/robots.zip", :type=>"application/zip"
And download link:
<%= link_to "download", download_path %>
So, when i'm clicking on the link I get this:
Image http://joxi.ru/GrqvQ3xHlbaYmz.jpg
It seems like the browser is trying to show zip file content... But when I open the action from my browser (or refresh page) it's working fine.
Why doesn't it work when I click on the link generated by link_to?

Adding disposition: "inline" instead of disposition: "attachment" worked for me.

Send_file open file in browse instead of download it
Try with this
def download
send_file("#{Rails.root}/public/downloads/robots.zip", :type=>"application/zip",:disposition=> "attachment; filename=robots.zip")
end
and
<%= link_to "download", download_path ,target: '_self' , data-turbolinks="false" %>

Related

How can I add a download link to files in my Digitalocean Space in my Rails app?

I'm trying to create a download link/button in my Rails app so that users can download files from my Digitalocean Space. I want to be able to click a download button on my show page and download the file. Here is what I've tried s far:
samples_controller.rb
def show
#sample = Sample.find(params[:id])
data = open(#sample.audio_url)
#sample_download = send_data data.read, filename: #sample.audio.metadata["filename"], type: #sample.audio.metadata["mime_type"], disposition: 'inline'
end
show.html.erb
<%= link_to "Download Sample", #sample_download %>
When I load the show page this opens a page that plays the audio file. What I want is to see my show template as normal and be bale to click a link to download the file. Maybe I should have a separate download_sample method, I'm not sure.
You can trigger download on browser side using the download attribute on an <a> tag.
All you need is something like <a href="http://linktofile.com/audio.mp3" download>Download</a> and user can click on the link to download.
Using the Rails helper, you can do <%= link_to "Download", #sample.audio_url, download: true %>
Hi for content from another website make sure the link is a direct link.
If the data you want to download is from active storage take a look at rails_blob_path (here)
.
In your model
Class Sample < ApplicationRecord
has_one_attached :data
end
In your controller
class SampleController < ApplicationController
def show
#sample = Sample.find(params[:id])
#sample_download = rails_blob_path(#sample.data, disposition: 'attachment')
end
end
In your views
<%=link_to "Download", #sample_download %>

How to display the variant of an image with Active Storage in a JS box?

In my view, I can display my file attached to the model with Active Storage in a popup like this:
<%= image_tag #image.variant('small') %>
It's working well.
The problem is when I want to use a variant in the link:
<%= image_tag #image.variant('small') %>
The variant code used is:
file.variant(resize:size).processed.service_url
The link seems to be good, but when I click the image, the image is not opened in my JS popup as before but opened in a new browser window. This is very strange.
I shortened the link.
https://bucket.s3.eu-west-3.amazonaws.com/variants/MmsLY3rf8yR9/38a77a69d170464c472f6d36fb3fbc28b284af0cadaa533?response-content-disposition=inline%3B%20filename%3D%22chateau.jpeg%22%3B%20filename%2A%3DUTF-8%27%27chateau-lynch.jpeg&response-content-type=image%2Fjpeg&Signature=29fe7d85fe369ea2335fa8b333d4868d8c2f2c22e1efe
Is-it a "content-disposition" issue ?
Well, this is what I did:
In my Image model, I added an action and used the rails_representation_url() method from url_helpers:
include Rails.application.routes.url_helpers # need this for
include Rails.application.routes.url_helpers
def get_variant(version="high", disposition="attachment")
variant = file_variant(version)
return rails_representation_url(variant, only_path: true, disposition: disposition)
end
In my html, I can call my method with attachment disposition:
<a href="<%= #image.get_variant('high', 'attachment') %>" rel="example_group">
I can also download the image variant directly from my controller:
def download
redirect_to #image.get_variant('high', 'attachment')
end
If you want to only display the variant in the browser window, you can use 'inline':
redirect_to #image.get_variant('high', 'inline')
Not sure it's the best option, but it works.

Rails send_data doesn't save file

I'm using Nokogiri to generate an XML file in my app. I want to save this file, and I want to display a dialog box in which the user can select the folder in which download this file.
This is the action in my controller:
def download
require 'nokogiri'
if owner_signed_in?
#slips = current_owner.slips
builder = Nokogiri::XML::Builder.new do |xml|
xml.cedolini{
#slips.each do |slip|
xml.cedolino{
xml.codicecliente_ slip.client_code
xml.data_ slip.day.to_s
xml.ordini{
slip.product_slips.each do |order|
xml.ordine {
xml.codicearticolo_ order.product_code
xml.descrizionearticolo_ order.product_description
xml.ammontare_ order.amount.to_s
}
end
}
}
end
}
end
file = builder.to_xml
send_data file, :type => 'text/xml; charset=UTF-8;', :disposition => "attachment; filename=db.xml"
end
end
I have the route defined in this way:
get '/dbsinc/download'
When I call the action from the view, it doesn't save the XML, I see a new page with the url of my action, and in the page I see the XML file rendered on the page, but it doesn't open any dialog box to save the file.
Where am I getting wrong? Thanks
I think I figured out, route: `post 'dbsinc/download'
And in my view I defined the link in this way:
<%= link_to 'Download ', {controller:'dbsinc', action:'download'}, method: :post %>
And it works, the download dialog opens when I click on the link.

What happend after send_file in Rails controller?

In the index view, there is a link to download file:
<%= link_to filename, listing_download_path(:file => filename) %>
In the controller:
def download
pathname = File.join(USER_FOLDER, params[:file])
if File.file?(pathname)
send_file pathname
end
end
end
When the user click download, a file download popup is shown. What's happen after the file is downloaded? Does rails just sit there and do nothing more? If I delete the send_file line, dwonload.html.erb will be rendered. Does send_file skip view rendering?
What if I want to show soemthing like "You have downloaded ..."?
The send_file is a render itself. You could use the approach proposed in this question:
Rails: send_file never renders page or DoubleRender error
Basically your download link will send to a "success" view, from which you call the download file method automatically.

Download Image attached using Paperclip

Is there a way to make the users download the images attached using paperclip?
I just made a link like this:
link_to 'imagename', image.attachment.url
But this makes the browser open the image. I want the browser to ask the user to save the image. Is there a way to do that?
When it comes to sending a file you can find all information in here http://api.rubyonrails.org/classes/ActionController/Streaming.html There are most important cuts:
Simple download:
send_file '/path/to.zip'
Show a JPEG in the browser:
send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
Show a 404 page in the browser:
send_file '/path/to/404.html', :type => 'text/html; charset=utf-8', :status => 404
to use one of this option you have to create a new action in controller like this:
class SomeController < ApplicationController
def download_file
send_file image.attachment.path
end
end
Here's a simple solution using the HTML5 download attribute
<%= link_to image.name, image.attachment.url, download: image.attachment.original_filename %>

Resources