clement
clement

Reputation: 71

Send_file aws s3 carrierwave

I want to download a file uploaded on aws s3:

controller

def download
    send_file my_file.url
end

Actually I have tried all code found in similar posts:

send_file open(my_file.url).read

also without read. Nothing works

Upvotes: 3

Views: 1646

Answers (3)

2potatocakes
2potatocakes

Reputation: 2260

Another alternative would be to directly read from your carrierwave object and use send_data as opposed to send_file. You'll get the added benefit of carrierwave using it's own local cache if your file exists there as well.

Anyways you could try using send_data with your example like this:

send_data(my_file.file.read, filename: 'your_txt_file.txt', type: 'text/plain', disposition: 'attachment', stream: 'true', buffer_size: '4096')

You should specify disposition: 'attachment' if you're wanting the file to be directly downloaded by the user, or if you would prefer the file to render within the users own browser, you could use disposition: 'inline'

Upvotes: 3

Leszek Andrukanis
Leszek Andrukanis

Reputation: 2125

controller

def download
  redirect_to my_file.file.url
end

Upvotes: 1

Serge Pedroza
Serge Pedroza

Reputation: 2170

So this is the solution I ended up using for pfd, and zip files. Have not figured it out with images/png or jpeg. My model's name was Import. Hope this helps a bit.

View

<%= link_to 'Download', import.image.url %>

Controller

def download_file
  @import = Import.find(params[:id])
  send_file(@import.image.path,
      :disposition => 'attachment',
      :url_based_filename => false)
end

Routes

resources :imports do
  collection do
    get 'download'      
  end
end

Upvotes: 0

Related Questions