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?

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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

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) ========== put this in views=================   <p> <b>Upload file</b>     <form action="/file_uploads/upload_action" id="upload_form" method="post" enctype="multipart/form-data">       <input type="file" name="uploaded_file" />       <input type="submit" value="Upload" />     </form>   </p>

============ 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

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.)