Class of uploated file


#1

Hi, i have small problem when you upload file by input type=file to
rails then in rails it is represented by one of following StringIO,
File, Tempfile

Is it all? Or can it be represented by also other class?

I need to know this because i have my own validation of file kind and
when is mime type invalid then i want to put msg to record.errors and i
also want to dont upload this file in next step by my upload method so i
call in validation on invalid type somethig like

if file.is_a?(StringIO) then
				file.string = ''
				file.close
				file = nil
				record.errors.add attr, 'STRINGIO'
			elsif file.is_a?(File) then
				File.unlink(file.path)
				file = nil
				record.errors.add attr, 'FILE'
			elsif file.is_a?(Tempfile)
				record.errors.add attr, 'TEMPFILE'
				File.unlink(file.path)
				file = nil
			else
				record.errors.add attr, 'Some orher class'+file.class.to_s
				file = nil
			end

#2

On Feb 1, 2006, at 1:31 PM, stuff wrote:

also want to dont upload this file in next step by my upload method
file = nil
record.errors.add attr, ‘FILE’
elsif file.is_a?(Tempfile)
record.errors.add attr, ‘TEMPFILE’
File.unlink(file.path)
file = nil
else
record.errors.add attr, ‘Some orher class’+file.class.to_s
file = nil
end

You don’t need to care about the class of the file, just if it
responds to :read or not.

data = nil
data = file.read if file.respond_to? :read

You also don’t need to worry about unlinking anything. Rails will
take care of that for you if it is appropriate.


Eric H. - removed_email_address@domain.invalid - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com