File upload failed if png file less than 1kb

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

Havent experienced that, but have you tried attachment_fu plugin?

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
[email protected]

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)
http://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

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.
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs