# Problems about uploading file

**URL:** <https://www.ruby-forum.com/t/problems-about-uploading-file/124853>\
**Category:** Ruby\
**Created:** [January 3, 2008, 4:48am UTC](https://www.ruby-forum.com/t/problems-about-uploading-file/124853 "2008-01-03T04:48:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![2tcreative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/a3d4f5/32.png) [@2tcreative](https://www.ruby-forum.com/u/2tcreative)\
**Post date:** [January 3, 2008, 4:48am UTC](https://www.ruby-forum.com/t/problems-about-uploading-file/124853/1 "2008-01-03T04:48:19Z")

</div>

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,  
:orginazation, :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
```

---

<div class="post-metadata">

**Author:** ![2tcreative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/a3d4f5/32.png) [@2tcreative](https://www.ruby-forum.com/u/2tcreative)\
**Post date:** [January 3, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/problems-about-uploading-file/124853/2 "2008-01-03T17:15:47Z")

</div>

On 3 Jan 2008, at 03:47, æ»•ç¥¥åŠŸ wrote:

> I am working on some upload file code , here it is :

It looks like you’re using Rails, in which case you might save  
yourself some work with the attachment\_fu plugin:

[http://svn.techno-weenie.net/projects/plugins/attachment\_fu/](http://svn.techno-weenie.net/projects/plugins/attachment_fu/)

And if you are using Rails you’ll probably receive more responses on  
the Rails mailing list:

[http://groups.google.com/group/rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk)

Sorry not to have answered your question directly!

Regards,  
Andy S.
