# File upload failed if png file less than 1kb

**URL:** <https://www.ruby-forum.com/t/file-upload-failed-if-png-file-less-than-1kb/142070>\
**Category:** Rails\
**Created:** [July 3, 2008, 5:52am UTC](https://www.ruby-forum.com/t/file-upload-failed-if-png-file-less-than-1kb/142070 "2008-07-03T05:52:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![betame342](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/90db22/32.png) [@betame342](https://www.ruby-forum.com/u/betame342)\
**Post date:** [July 3, 2008, 5:52am UTC](https://www.ruby-forum.com/t/file-upload-failed-if-png-file-less-than-1kb/142070/1 "2008-07-03T05:52:25Z")

</div>

Hi,

Has anyone experience file uploading in failed if file size less than  
1kb for image. In this case png. I did try larger png and it was  
successful.

It failed at the time copy the file from the uploaded directory path.  
(that 1kb did not get uploaded. as a result, only new file with size  
zero created). I did use “b” which set it to binary mode.

f = File.new(@target\_dir+file\_name, “wb”)  
FileUtils.copy uploaded\_file.path, f.path

Thank you in advance,  
Beta

---

<div class="post-metadata">

**Author:** ![betame342](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/90db22/32.png) [@betame342](https://www.ruby-forum.com/u/betame342)\
**Post date:** [July 3, 2008, 9:29am UTC](https://www.ruby-forum.com/t/file-upload-failed-if-png-file-less-than-1kb/142070/2 "2008-07-03T09:29:57Z")

</div>

Havent experienced that, but have you tried attachment\_fu plugin?

---

<div class="post-metadata">

**Author:** ![betame342](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/90db22/32.png) [@betame342](https://www.ruby-forum.com/u/betame342)\
**Post date:** [July 3, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/file-upload-failed-if-png-file-less-than-1kb/142070/3 "2008-07-03T17:35:54Z")

</div>

On Jul 2, 2008, at 11:52 PM, Beta B. wrote:

> f = File.new(@target\_dir+file\_name, “wb”)  
> FileUtils.copy uploaded\_file.path, f.path
> 
> Thank you in advance,  
> Beta

Small uploads are not put into a file, you might get a StringIO rather  
than a file.

In either case you can call the .read method on the “file” that comes  
back from the form.

If this doesn’t help you figure out your problem, post some code.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![betame342](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/90db22/32.png) [@betame342](https://www.ruby-forum.com/u/betame342)\
**Post date:** [July 4, 2008, 1:54am UTC](https://www.ruby-forum.com/t/file-upload-failed-if-png-file-less-than-1kb/142070/4 "2008-07-04T01:54:59Z")

</div>

Thanks for the feedback. (I did not use attachment fu because I just  
want simple upload without creating model or resizing image)

starting from scratch, I am able to reproduce the problem (Rails 2.0.2)

The problem in “uploaded\_file.path” is nil if file less then 1kb

To test it, try this file below (can use this if the attachment fail)  
[![](https://www.google.com/images/x3.png) ](https://www.google.com/images/x3.png)

========== put this in views=================

**Upload file**

============ put this in controller =====================  
def upload\_action  
uploaded\_file = params[:uploaded\_file]

```
file_name = uploaded_file.original_filename
@target_dir = "#{RAILS_ROOT}/public/assets/"
unless File.directory?(@target_dir)
  FileUtils.mkdir_p(@target_dir)
end
f = File.new(@target_dir+file_name, "wb")

FileUtils.copy uploaded_file.path, f.path

```

end

======= put this one in controller , in case, if it ask for authenticity  
token: (this for testing purpose) =========  
protect\_from\_forgery :secret =\> ‘webgen’, :only =\> :index

# ====================================================== == Trying file read method as suggested by Rob B.=========

The reason why it failed because “uploaded\_file.path” is nil

---------------- this below also failed----------  
def upload\_action3  
# Do stuff with params[:uploaded\_file]  
uploaded\_file = params[:uploaded\_file]

```
file_name = uploaded_file.original_filename
@target_dir = "#{RAILS_ROOT}/public/assets/"
unless File.directory?(@target_dir)
  FileUtils.mkdir_p(@target_dir)
end
f = File.new(@target_dir+file_name, "wb")

file_data = open(uploaded_file.path, "r")
file_data.close

```

end

---

<div class="post-metadata">

**Author:** ![betame342](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/90db22/32.png) [@betame342](https://www.ruby-forum.com/u/betame342)\
**Post date:** [July 4, 2008, 3:43am UTC](https://www.ruby-forum.com/t/file-upload-failed-if-png-file-less-than-1kb/142070/5 "2008-07-04T03:43:45Z")

</div>

If you want simple, look at the Paperclip plugin. It’s good to know how  
to  
do this yourself, but once you know you can, it’s better to use  
existing,  
tested code than to struggle with an error that’s impeding your progress  
(at  
least that’s my opinion.)

On Thu, Jul 3, 2008 at 6:54 PM, Beta B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
