views:

34

answers:

1

I am trying to upload an Image.

The image file gets created but the size of the file is 0 bytes.

Here is the code in controller

name =  params[:comparison][:image1].original_filename
name_content_type = params[:comparison][:image1].content_type
directory = "public/images"
path = File.join(directory, name)
file = File.open(path, "wb") { |f|
         content = params[:comparison][:image1].read
         f.write(content)
       }
# also tried the below but doesn't work
# file = File.new(path, "wb") { |f| f.write(params[:comparison][:image1].read) }
# RAILS_DEFAULT_LOGGER.info(file) is showing 0

Can anyone point me to working demo with code? I know this is such a simple stuff! not sure why its not working...!!!!

A: 

Are you using <form enctype="multipart/form-data"... ?

Lucho
yes..I am using multipart form! also I can see the uploaded temp file in /tmp/RackMultipart20101028-31385-16jyoow-0 .
Madhusudhan
Is it possible that the images folder is not writable? What version of rails are you using?
Lucho
i m using rails 2.3.8 ... I have given the folder 777 permission! it has full permission...
Madhusudhan
It is not easy to find the problem, but you can double check that you are on the correct way on this page: http://guides.rubyonrails.org/form_helpers.html
Lucho
well, not sure why its not working! double checked with the "Uploading files" section... the uploaded image is still of size 0 bytes!! if you come across a working demo of file upload with code, please post a link...
Madhusudhan