Rails 关于用 carrierwave 上传文件到 GridFS 中,获取文件大小的问题

bindiry · December 26, 2011 · Last by bindiry replied at February 04, 2012 · 3356 hits

我用 carrierwave 实现上传文件到 GridFS 中,通过页面中调用 model.file_url 来获取文件 url 并下载。

想请教,怎样来获取 GridFS 中文件的大小?

找到了 model.file.length

在这里找到的 http://rubydoc.info/github/jnicklas/carrierwave/frames CarrierWave -> Uploader -> Base -> Method Summary

一种鹾点的方法是用 params[:Filedata].tempfile.length

@attachment.file_size = params[:Filedata].tempfile.length

还有种是:

class Attachment
  before_create :store_image_attributes
  def store_image_attributes
    if self.image.present?
      self.file_size = self.image.file.size
    end
  end
end
You need to Sign in before reply, if you don't have an account, please Sign up first.