回形针附件文件大小

回形针附件文件大小

本文介绍了回形针附件文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取回形针附件的每种样式的文件大小?

How do I fetch the file size of each style of a paperclip attachment?

@user.attachment_file_size似乎不起作用

@user.attachment(:style).size

给出与实际文件大小无关的数字

gives a number not related to the actual file size

推荐答案

除了原始样式,我没有找到如何获取给定样式的文件大小.

I didn't find either how to get the file size for a given style, other than the original one.

如回形针源代码中所示 @user.attachment.size返回最初分配的文件大小.无法针对特定样式获取它...

As seen in the paperclip source code, @user.attachment.size returns the size of the file as originally assigned. There is no way to get it for a specific style...

解决方案是:

open(@user.attachment(:style)).size

但根本没有效率.

要做得好,您可能应该在附件表中添加一些自定义尺寸"字段,一旦为每种样式保存了附件,这些字段就会填充...

To do it well, you should probably add some "custom size" fields in your attachment table that you would fill once the attachment is saved for each style...

这篇关于回形针附件文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:02