4

有人可以启发我如何下载文件send_file吗?

image.jpg里面有一个文件app/assets/images。我在我的控制器中试过这个:

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'

这里可能有什么问题?谢谢!

4

5 回答 5

9

尝试:

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

def download
  send_file(File.join(IMAGES_PATH, "image.jpg"))
end
于 2014-02-25T00:38:21.717 回答
4
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......
于 2014-11-20T07:53:59.033 回答
3

好吧,我建议您将文件移动到公共文件夹。无论如何,做这个

send_file(Rails.root.join('app' , 'assets', 'images', 'image.jpg'))
于 2014-02-25T00:45:10.307 回答
2

我们需要指定地雷类型以便它缓存。

send_file ("#{Rails.root}/public"+image.image_path), :type => "image/jpeg", :disposition => 'inline'
于 2014-09-23T15:51:57.070 回答
2

对于仍在寻找答案的任何人,send_data 和 send_file 在响应 ajax 调用时将不起作用。相反,请尝试提交表单或使用<a href=..>来调用控制器方法并下载文件。

于 2017-06-01T13:14:31.897 回答