我正在使用Rails 5和ruby-filemagic gem。如何从上传的文件中获取二进制数据?我的表单看起来像这样
<%= form_for @person, html: { multipart: true } do |f| %>
...
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
我的控制器如下
def create
@person = Person.new(person_params)
if @person.image
cur_time_in_ms = DateTime.now.strftime('%Q')
file_location = "/tmp/file#{cur_time_in_ms}.ext"
File.binwrite(file_location, @person.image.tempfile)
fm = FileMagic.new(FileMagic::MAGIC_MIME)
@person.content_type = fm.file(file_location, true)
end
if @person.save
redirect_to @person
else
# This line overrides the default rendering behavior, which
# would have been to render the "create" view.
render "new"
end
end
但是当我查看我的文件内容(我已复制到“/ tmp”目录中)时,它会显示...
#<ActionDispatch::Http::UploadedFile:0x007fba2573bbd0>
如何获取二进制数据(最好是“@ person.image”属性)?
答案 0 :(得分:0)
点击p @person.image.methods
应该会产生你想要的东西(我猜#read
)