Comment télécharger un fichier avec send_file ?
-
20-12-2019 - |
Question
Quelqu'un peut-il m'éclairer, comment puis-je télécharger un fichier avec send_file
?
j'ai un fichier image.jpg
à l'intérieur app/assets/images
.J'ai essayé ceci dans mon contrôleur :
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
Pour chaque chemin, il est indiqué :
ActionController::MissingFile in HomeController#download
Cannot read file 'some_path'
Quel pourrait être le problème ici ?Merci!
La solution
Essayer:
IMAGES_PATH = File.join(Rails.root, "public", "images")
def download
send_file(File.join(IMAGES_PATH, "image.jpg"))
end
Autres conseils
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......
eh bien, je vous suggère de déplacer votre fichier vers un dossier public.Quoi qu'il en soit, fais ça
send_file(Rails.root.join('app' , 'assets', 'images', 'image.jpg'))
Nous devons spécifier le type de mine pour qu'elle soit mise en cache.
send_file ("#{Rails.root}/public"+image.image_path), :type => "image/jpeg", :disposition => 'inline'
Pour tous ceux qui recherchent encore une réponse, send_data et send_file ne fonctionneront pas pour répondre aux appels ajax.Essayez plutôt de soumettre un formulaire ou d'utiliser <a href=..>
pour appeler la méthode du contrôleur et télécharger un fichier.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow