Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 448
user3339562 Avatar asked Nov 30 '22 02:11

user3339562


2 Answers

Try:

IMAGES_PATH = File.join(Rails.root, "public", "images")

def download
  send_file(File.join(IMAGES_PATH, "image.jpg"))
end
like image 72
Coenwulf Avatar answered Dec 07 '22 00:12

Coenwulf


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......
like image 21
Mukesh Avatar answered Dec 07 '22 00:12

Mukesh