Re: file uploading with ruby made easy

Daniel Bretoi <[email protected]>
Newsgroups gmane.comp.apache.mod-ruby
Message-ID <[email protected]>
thanks guys. I'll have to look at this again later. I was deviated.

db
On Fri, Jan 23, 2004 at 04:03:01PM -0800, paul vudmaska wrote:
> Uploading was not as hard as I had thot! Attached is a
> file that should get you going uploading and included
> in text below....good luck !
> 
> upload.rbx
> 
> =begin
> 
> 	upload.rbx is meant to be illustrative and might not
> serve your purposes but should 
> 	get you started if you need to upload with
> Ruby/modruby
> 
> 	{{{http://www.rubycentral.com/book/lib_network.html
> 	
> 	
> 	Multipart Form Values
> 
> 	When dealing with a multipart form, 
> 	the array returned by CGI#[] is composed of objects
> of class Tempfile, 
> 	with the following dynamically added methods: 
> 	
> 		original_filename
> 		local_path
> 		content_type
> 		size
> 		
> 	To make this file work you'll need to...
> 	
> 		make it executable
> 		make your upload directory writable(Upload::PATH)
> 		
> 		...and that _should_ be it
> 		
> 	}}}
> 	
> =end
> 
> 
> begin
> 
> 	def main
> 	#{{{
> 		require 'cgi'
> 		
> 		$cgi = CGI.new("html4")
> 		
> 		$cgi.header('content-type'=>'text/html')
> 		
> 		puts '<html><body>'	
> 		
> 		if $cgi.params.length == 0 
> 		
> 			Upload.form 
> 		else 
> 
> 			Upload.post 
> 			
> 			print Upload.getStatus
> 			
> 		end
> 	
> 		puts '</body></html>'
> 		
> 	end#}}}
> 
> 	
> 
> rescue Exception=>e
> 
> 	puts e
> 
> end
> 
> class Upload
> #{{{ -----------------------Uploads file to server
> from html form--------------------------
> 	
> 	#max size of file
> 	MAX_SIZE     = 25000
> 	
> 	#where the file goes / MUST HAVE WRITE PRIVS HERE
> 	PATH	 	 = "/YOUR_FILE_PATH_HERE_PLEASE/"
> 	
> 	#how many file inputs - you can upload multiple files
> at once
> 	FILE_COUNT	 = 5
> 	
> 	#what file types do we allow?
> 	CONTENT_TYPES= ['image/jpg','image/gif']
> 	
> 	#how are things going?
> 	@@status 	 = []
> 	
> 	def self.form
> 	#{{{
> 	
> 		puts '<form method="post"
> enctype="multipart/form-data">'
> 		
> 		FILE_COUNT.times do 
> 		
> 			puts '<br/><input type="file" name="myfile">'
> 
> 		end
> 			
> 		 puts '<p/><input type="submit"></form>'	
> 	
> 	end#}}}
> 
> 	
> 	def self.post		
> 	#{{{
> 	
> 		$cgi['myfile'].each do |incoming|
> 			
> 			if incoming.size == 0
> 				
> 				@@status << "<br/>Avoiding empty field"
> 				next
> 			
> 			end
> 			
> 			if incoming.size > MAX_SIZE
> 				
> 				@@status << "<br/>Data too large for
> #{incoming.original_filename}(#{incoming.size} >
> #{MAX_SIZE})"
> 				next			
> 			
> 			end
> 			
> 			#need to strip :)...trailing space...ouch
> 			if not CONTENT_TYPES.include?
> incoming.content_type.strip
> 				
> 				@@status << "<br/>Type not allowed(type =
> #{incoming.content_type}) allowed content =
> #{CONTENT_TYPES.join(' | ')}"
> 				next			
> 			
> 			end			
> 			
> 			# all should be ok to upload
> 			path 	 = PATH + incoming.original_filename
> 			
> 			File.new(path.untaint,'w') do |file|
> 				
> 				file << incoming.read
> 			
> 			end
> 			
> 			@@status << "<br/>done writing #{path}"
> 		end	
> 	
> 	end#}}}
> 	
> 	def self.getStatus
> 	#{{{
> 		
> 		@@status
> 	
> 	end#}}}
> end#}}}
> 
> 
> 
> main
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free web site building tool. Try it!
> http://webhosting.yahoo.com/ps/sb/


-- 
"The two aims of the Party are to conquer the whole surface of the earth
and to extinguish once and for all the possiblity of independant thought."
-- 1984, George Orwell
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.