From: Martin Stannard Date: 2004-02-27T08:13:28+09:00 Subject: Re: CGI and multipart data Hi Martin, > > Hi all, > > I have a single HTML form which contains simple text controls as well as > file > upload controls. I had assumed that the CGI library would return me items > of > type String for the text boxes and items of type Tempfile for the file > upload > boxes.... > > However, it appears (after searching the archives) that StringIO is used > instead of Tempfile on ruby 1.8 (in some cases and not others?), and that > as > soon as you specify an enctype of multipart you never? receive Strings but > always receive either Tempfile or StringIO. > > This is what i really want from the CGI library: > * CGI returns *everything* as a string except for file uploads - which are > returned as Tempfiles > > Assuming that I cannot have that :-) can somebody in the know please tell > me > (or point me to some documentation)... The source for cgi.rb is a good place to start I found. > > 1. how can I tell whether or not the supplied parameter was a file upload > or > simple text that has been passed to me as a Tempfile/StringIO? At the > moment > I don't know whether to copy the file or not - because I don't know if it > is > a valid file or just another parameter. Can't you tell by the name of the parameter? If you're naming them then you should know what to expect. Anyway it doesn't matter cause you treat them exactly the same. See pt 4 below. > > 2. do hidden controls work with multi-part forms? there is some > discussion of > them not working on the mailing list dating back to last November but I > don't > know if that has been fixed or not. My limited testing here indicates > that > hidden parameters are ignored on multipart forms? > You may want to look at http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/87163 if you're having trouble with multiple fields on multi-part forms. I use hidden controls with no problems. > 3. why are all values expressed as Tempfile/StringIO when using a > multipart > form? Why not just have the file uploads as tempfiles? > > 4. how does the CGI library determine whether or not to switch between > Tempfile/StringIO > I use the following method to extract data when using multi-part forms: def get_element_data(name) @t << "get_element_data [#{name}]" << "
" value = @cgi[name] data = value.read @t << "value.read : [#{data}]
" data end From my understanding the read method is mixed in regardless of whether the passed item is a Tempfile or a StringIO so it shouldn't matter to the developer. > 5. do I have to specifically close the IO object (assuming that one is > returned?) - or perhaps I do not have to close the StringIO but I do have > to > close the Tempfile? I haven't been doing this. > > 6. are all of the form parameters in different tempfiles or are they all > in > one? Say i close the file after reading 1 argument ... does this mean > that I > cannot read any more? Haven't done multiple file uploads from 1 form. > > Thanks for any help you can give me - I am pretty stuck on this. If I get > code working with StringIO objects it seems to break when I submit large > files, and if I get code working with Tempfile it seems to break when I > submit small files :-) > > Cheers, > Martin > > > -- > Martin Hart > Arnclan Limited > 53 Union Street > Dunstable, Beds > LU6 1EX > http://www.arnclanit.com > > Hope that helps, regards, Martin