Ruby on rails 获取carrierwave上载的文件大小

Ruby on rails 获取carrierwave上载的文件大小,ruby-on-rails,upload,carrierwave,Ruby On Rails,Upload,Carrierwave,我正在尝试获取carrierwave上载的文件大小。我正在尝试做一些事情,例如: <%= upload.file.size %> 及 两者都不起作用。我也在我的上传上运行了:方法,但没有看到任何会导致我知道上传文件大小的东西。如何获取上传的文件大小?不幸的是,carrierwave没有提供存储上传后确定上传文件大小的方法(因为它不知道,唯一的方法是重新访问该文件并检查其文件大小)。您可以在此处使用两种不同的场景: 创建一个助手,它将从存储中检索文件并确定文件的大小(这可能很

我正在尝试获取carrierwave上载的文件大小。我正在尝试做一些事情,例如:

<%= upload.file.size %>



两者都不起作用。我也在我的上传上运行了:方法,但没有看到任何会导致我知道上传文件大小的东西。如何获取上传的文件大小?

不幸的是,carrierwave没有提供存储上传后确定上传文件大小的方法(因为它不知道,唯一的方法是重新访问该文件并检查其文件大小)。您可以在此处使用两种不同的场景:

  • 创建一个助手,它将从存储中检索文件并确定文件的大小(这可能很棘手,我个人不会这么做)

  • 上传文件后,记录文件的大小,并将其存储为模型的属性(有一个指南介绍如何执行此操作)


  • 答案是这样的。CarrierWave并没有专门提供给您,因此您必须做好以下工作:

    number_to_human_size(object.attachment.file.size)
    

    /app/uploaders/attachment\u file\u uploader.rb

    class AttachmentFileUploader < CarrierWave::Uploader::Base
      include CarrierWave::MimeTypes
      include ActionView::Helpers::NumberHelper
    
      storage :file
    
      process :set_content_type
      process :store_file_attributes
    
      ....
    
      private
    
      def store_file_attributes
        if file && model
          model.file_name  = File.basename(file.file)
          model.file_size  = File.size(file.file)
          model.human_size = number_to_human_size(model.file_size)
        end
      end
    
    end
    
    class AttachmentFileUploader
    迁移:

    class CreateAttachments < ActiveRecord::Migration
    
      def change
        create_table :attachments do |t|
          t.references :attachable, polymorphic: true
          t.string     :file
          t.string     :file_name
          t.integer    :file_size
          t.string     :human_size
          t.string     :description
    
          t.timestamps null: false
        end
      end
    
    end
    
    class CreateAttachments
    这已经太晚了,但我想我应该为未来的观众添加

    如果要与RMagick和MiniMagick一起使用,请访问链接

    如果在自定义处理器中将尺寸分配给模型(已将上载程序安装到该模型),则在上载时,该尺寸将与图像路径一起保存

    class ImageUploader[500500]
    过程:存储尺寸
    结束
    私有的
    def存储单元尺寸
    if文件和模型(&M)
    model.width,model.height=`标识格式“%wx%h”#{file.path}`.split(/x/)
    结束
    结束
    结束
    
    class CreateAttachments < ActiveRecord::Migration
    
      def change
        create_table :attachments do |t|
          t.references :attachable, polymorphic: true
          t.string     :file
          t.string     :file_name
          t.integer    :file_size
          t.string     :human_size
          t.string     :description
    
          t.timestamps null: false
        end
      end
    
    end
    
    class ImageUploader < CarrierWave::Uploader::Base 
      process :store_dimensions
    
      # If you like, you can call this inside a version like this
      # instead of at the top level.
      # That will store the dimensions for this version.
      version :show do
        process :resize_to_limit => [500, 500]
        process :store_dimensions
      end
    
      private
    
      def store_dimensions
        if file && model
          model.width, model.height = `identify -format "%wx%h" #{file.path}`.split(/x/)
        end
      end
    end