Forum: Ruby on Rails Class of uploated file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
stuff (Guest)
on 2006-02-01 23:31
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
Eric H. (Guest)
on 2006-02-02 21:59
(Received via mailing list)
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
This topic is locked and can not be replied to.