Problems saving an uploaded image to an app folder

In my app, the user can upload images. Rather than shoving the image
data in the database, i want to put the filename and content type into
the db, then rename the image with its record’s id number, and then save
it into a local folder in my app folder. (hardcoded for now)

All the details are being saved to the db ok, so as far as i know the
“picture_file=” method is fine. But when i try to write the image with
write_to_disc i have a problem: if i get the ‘read’ field from the
uploaded image, store it in a variable, and try to write it with another
method, i get an error saying “no ‘write’ method exists for String”:
can anyone see what i’m doing wrong here? (i’ve just enclosed the
model methods as all the controller does is call ‘write_to_disc’ on my
picture instance):

#in class Picture

attr_accessor :file_data

def picture_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.file_data = input_data.read
end

def write_to_disc
#save the picture to newspipe/data/pictures with the name
‘#{self.id}.extension’
new_filename = “#{self.id}.#{self.content_type.split(”/").last}"
if
self.file_data.write(“C:\code\InstantRails\rails_apps\newspipe\data\pictures\data\pictures\#{new_filename}”)
return true
else
return false
end
end

You need to create StringIO or TempFile object for storing input_data

On 8/3/07, Max W. [email protected] wrote:

method, i get an error saying “no ‘write’ method exists for String”:
self.content_type = input_data.content_type.chomp
else
return false
end
end

Posted via http://www.ruby-forum.com/.


Cheers!

Pratik Naik wrote:

You need to create StringIO or TempFile object for storing input_data

Thanks Pratik - i just tried that after reading your post, but probably
did it wrong - i get a complaint about “uninitialized constant
Picture::TempFile” (i’ve done require ‘tempfile’ in the model by the
way)

The first method is causing the crash now - am i using TempFile wrong?

def picture_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.file_data = TempFile.new(input_data)
end

def write_to_disc
#save the picture to newspipe/data/pictures with the name
‘#{self.id}.extension’
new_filename = “pic#{self.id}.#{self.content_type.split(”/").last}"
self.file_data.write(“C:/code/InstantRails/rails_apps/newspipe/data/pictures/#{new_filename}”)
end

Pratik Naik wrote:

Umm…try

self.file_data = ::TempFile.new(input_data)

done that - i’m still getting
“uninitialized constant TempFile” back though.

thanks for the help by the way…

Umm…try

self.file_data = ::TempFile.new(input_data)

On 8/3/07, Max W. [email protected] wrote:

The first method is causing the crash now - am i using TempFile wrong?
new_filename = “pic#{self.id}.#{self.content_type.split(”/").last}"
self.file_data.write(“C:/code/InstantRails/rails_apps/newspipe/data/pictures/#{new_filename}”)
end

Posted via http://www.ruby-forum.com/.


Cheers!

http://manuals.rubyonrails.com/read/chapter/56

this helped me get files into the file system using:

File.open("#{RAILS_ROOT}/path/to/storage/#{new_filename}.jpg", “wb”)
do |f|
f.write(@params[‘picture_file’].read)
end

then you can access all the files from within your app instead of
writing to your local disk

and of course you can change the file extension to take any kind of
picture

[email protected] wrote:

http://manuals.rubyonrails.com/read/chapter/56

this helped me get files into the file system using:

File.open("#{RAILS_ROOT}/path/to/storage/#{new_filename}.jpg", “wb”)
do |f|
f.write(@params[‘picture_file’].read)
end

then you can access all the files from within your app instead of
writing to your local disk

and of course you can change the file extension to take any kind of
picture

That’s great, thanks tyler!