From: Matthew Margolis Date: 2005-01-26T05:44:20+09:00 Subject: Re: ruby cgi and forms question Ok that worked. Now my problem is just getting the uploaded image into a file. I am trying to do uploadImage = cgi['upload'] if uploadImage ucomments = cgi.params['uploadcomment'] ucomments.each {|com| com.gsub!(" ", "_")} image = File.new("#{ucomments}.jpg", "w+") image << uploadImage image.close end But I always end up with a 0 length .jpg though it is named right. cgi['upload'] is StringIO. -Matthew Margolis Belorion wrote: >If I recall correctly, multi-part form data comes as type StringIO, >not String. Check out >http://www.ruby-doc.org/stdlib/libdoc/stringio/rdoc/ for RDoc >documentation on StringIO. You can probably get the string by >StringIO.read. > >require 'stringio' >a = StringIO.new( "foo") >a.read > >will give "foo" > > >On Wed, 26 Jan 2005 02:55:14 +0900, MATTHEW REUBEN MARGOLIS > wrote: > > >>I have a form that contains both text fields and a file field for >>uploading. The form is set to post to a ruby script on my server. The >>script works(excluding file uploading) when I delete >>ENCTYPE="multipart/form-data" from the form tag but if that is >>present(and it needs to be for the image upload to work) then ruby cgi >>chokes on a gsub for one of my text field parameters called comment. >> >>imageadmin.rb:14: private method `gsub!' called for >># (NoMethodError) >> >>Anyone have some ideas for how I can make this work? >> >>Here is the form code (it is from an eruby file) >>----------------------------------------------------- >>% imageArray = Dir.glob("*.jpg").sort! >>% commentArray = imageArray.collect {|image| image[4..-5]} >>% commentArray.collect {|image| image.gsub!("_", " ")} >> >>
>><%imageArray.length.times do |count|%> >> >>
>> Comment:>value="<%=commentArray[count]%>"> >>
>> Number:>value="<%=imageArray[count][0..2] %>"> >>
>> Delete:>value="<%=imageArray[count]%>"> >>
>><%end%> >>
>>
>> >>Comment: >> >>
>> >>
>>----------------------------------------------------------------- >> >>And here is the script itself. I apologize for the lack of comments and >>that it isn't as short as it could be. I am going to clean it up tonight. >>----------------------------------------------------------------------- >>#!/usr/bin/env ruby >>$stderr = File.open("error.log", "w+") >>print "Content-type: text/html\n\n" >>require 'cgi' >>cgi = CGI.new >> >>deleteArray = cgi.params['delete'] >>imageArray = Dir.glob("*.jpg").sort! >>oldArray = Dir.glob("*.jpg").sort! >>commentArray = cgi.params['comment'] >> >>#####Here is where I get the error##### >>commentArray.each {|com| com.gsub!(" ", "_")} >>numberArray = cgi.params['order'] >> >>count = imageArray.length >> >>count.times do |x| >> imageArray[x].sub!(/^\d\d\d/, numberArray[x]) >>end >> >>count.times do |x| >> imageArray[x] = numberArray[x] + "_" + commentArray[x] + ".jpg" >>end >> >>count.times do |x| >> File.rename(oldArray[x], imageArray[x]) >>end >> >>#uploadImage = cgi['upload'] >>#if uploadImage >># ucomments = cgi.params['uploadcomment'] >># ucomments.each {|com| com.gsub!(" ", "_")} >># image = File.new("#{ucomments}.jpg", "w+") >># image << uploadImage >># image.close >>#end >> >>for thing in deleteArray >> File.delete(thing) >>end >> >>print "" >> >>------------------------------------------------------------------- >> >>Thank you, >>Matthew Margolis >> >> >> >> > > >