I am not using carrierwave or paperclip for my file upload. I want to find out the file extension and the size in bytes of a file that the user has uploaded? Any ideas on how I can achieve this?
我没有使用carrierwave或paperclip来上传文件。我想找出文件扩展名和用户上传的文件的大小(以字节为单位)?关于如何实现这一目标的任何想法?
3 个解决方案
#1
19
File.size("#{doc.filename}")
Just throw the name of the file into the curly brackets and you should be set.
只需将文件名称放入大括号中即可设置。
If you want KB/MB use:
如果您想要KB / MB使用:
number_to_human_size(File.size("#{doc.filename}"))
EDIT:
编辑:
You can use the exact path or Pathname
您可以使用确切的路径或路径名
1.9.3p125 :005 > x=Pathname.new("/usr/bin/ruby")
=> #<Pathname:/usr/bin/ruby>
1.9.3p125 :006 > File.size(x)
=> 5488
For extension:
对于扩展:
File.extname("test.rb") #=> ".rb"
#2
6
params[:file].size
File.extname(params[:file].original_name)
or params[:file].original_name.match(/\.(\S*)$/).try(:"[]",1)
或params [:file] .original_name.match(/ \。(\ S *)$ /)。try(:“[]”,1)
#3
0
You can use Ruby's File class methods.
您可以使用Ruby的File类方法。
- File.size
- 文件大小
- File.extname
- File.extname
#1
19
File.size("#{doc.filename}")
Just throw the name of the file into the curly brackets and you should be set.
只需将文件名称放入大括号中即可设置。
If you want KB/MB use:
如果您想要KB / MB使用:
number_to_human_size(File.size("#{doc.filename}"))
EDIT:
编辑:
You can use the exact path or Pathname
您可以使用确切的路径或路径名
1.9.3p125 :005 > x=Pathname.new("/usr/bin/ruby")
=> #<Pathname:/usr/bin/ruby>
1.9.3p125 :006 > File.size(x)
=> 5488
For extension:
对于扩展:
File.extname("test.rb") #=> ".rb"
#2
6
params[:file].size
File.extname(params[:file].original_name)
or params[:file].original_name.match(/\.(\S*)$/).try(:"[]",1)
或params [:file] .original_name.match(/ \。(\ S *)$ /)。try(:“[]”,1)
#3
0
You can use Ruby's File class methods.
您可以使用Ruby的File类方法。
- File.size
- 文件大小
- File.extname
- File.extname