send_fileでファイルをダウンロードする方法
-
20-12-2019 - |
質問
誰かが私に啓発することができますか?
send_file
の内部にはファイルimage.jpg
があります。私は私のコントローラーでこれを試してみました:
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
.
各パスの場合:
ActionController::MissingFile in HomeController#download
Cannot read file 'some_path'
.
ここで問題があるかもしれませんか?ありがとう!
解決
試してみてください:
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......
. まあ、ファイルをパブリックフォルダに移動することをお勧めします。とにかく、この
を行いますsend_file(Rails.root.join('app' , 'assets', 'images', 'image.jpg'))
. キャッシュするように鉱山タイプを指定する必要があります。
send_file ("#{Rails.root}/public"+image.image_path), :type => "image/jpeg", :disposition => 'inline'
. まだ答えを探している人のために、send_dataとsend_fileはAjax呼び出しに応答するとうまくいきません。代わりに、フォームを送信したり、Controllerメソッドを呼び出したり、ファイルをダウンロードしたりしてください。
所属していません StackOverflow