Ë
ëøÏ鹦
Hi guys,
I am working on some upload file code , here it is :
================== code begin =================
require 'fileutils'
require 'uuid'
class Content < ActiveRecord::Base
include FileUtils
include UUID
before_create :write_file_to_disk
before_update :write_file_to_disk
......... ................
def update_attributes_from_params params, *args
args.each do |attribute|
s= params[attribute].blank? ? "self.#{attribute.to_s}=%Q{}"
:
%Q{self.#{attribute.to_s}="#{params[attribute]}"}
eval(s)
end
update
end
......... ..............
private
def write_file_to_disk
file_path = "#{upload_file_path}/#{user_id}/#{file_name}"
p " the file size : #{file.size}"
File.open(file_path, "wb") do |media_file|
media_file.write file.read
end
self.size = @file.size
end
def file= file
return if file.blank?
@file = file
ext_name = File.extname file.original_filename
# original_file_name and file_name are both content attributes
original_file_name = file.original_filename
file_name = UUID.new + ext_name
end
end
when I update an file, I do it like this :
@content = Content.find params[:content_id]
@content.update_attributes_from_params params, :title, rginazation, :file
and it produces :
"the file size : 8477823"
but when I check the file uploaded ,
irb >> File.size"/home/tim/upload_test/1/adb12d60-9bd5-012a-a458-000c29f18cec.mp3"
=> 2176
it's only 2.1K, so any body can tell me why and how to handel it correctly.
I tried @content.send("file=",params[:file]),but the real size is still 2176.
Thanks, regards!
Tim
I am working on some upload file code , here it is :
================== code begin =================
require 'fileutils'
require 'uuid'
class Content < ActiveRecord::Base
include FileUtils
include UUID
before_create :write_file_to_disk
before_update :write_file_to_disk
......... ................
def update_attributes_from_params params, *args
args.each do |attribute|
s= params[attribute].blank? ? "self.#{attribute.to_s}=%Q{}"
:
%Q{self.#{attribute.to_s}="#{params[attribute]}"}
eval(s)
end
update
end
......... ..............
private
def write_file_to_disk
file_path = "#{upload_file_path}/#{user_id}/#{file_name}"
p " the file size : #{file.size}"
File.open(file_path, "wb") do |media_file|
media_file.write file.read
end
self.size = @file.size
end
def file= file
return if file.blank?
@file = file
ext_name = File.extname file.original_filename
# original_file_name and file_name are both content attributes
original_file_name = file.original_filename
file_name = UUID.new + ext_name
end
end
when I update an file, I do it like this :
@content = Content.find params[:content_id]
@content.update_attributes_from_params params, :title, rginazation, :file
and it produces :
"the file size : 8477823"
but when I check the file uploaded ,
irb >> File.size"/home/tim/upload_test/1/adb12d60-9bd5-012a-a458-000c29f18cec.mp3"
=> 2176
it's only 2.1K, so any body can tell me why and how to handel it correctly.
I tried @content.send("file=",params[:file]),but the real size is still 2176.
Thanks, regards!
Tim