From: Jesper Olsen Date: 2003-11-04T00:43:49+09:00 Subject: Re: CGI uses file size to distinguish between regular values and files I'm using the CGI module for file upload - I think it works, it is just poorly documented. At least it works for me - I use it mainly for uploading binary files (jpg images) - typically in the range 1k-100k bytes in size - but also larger. Anyway, my code looks like this (upload.rbx) cgi=CGI.new if cgi.has_key?('image') image=cgi['image'][0].read end image is now a string representing the file that has been uploaded. This only works for multipart forms however - ie. the HTML page should look something like this:
... Or did I miss something? Cheers Jesper Austin Ziegler wrote: > On Mon, 3 Nov 2003 20:12:06 +0900, Dmitry Borodaenko wrote: > > On Mon, Nov 03, 2003 at 02:29:08PM +0900, Simon Kitching wrote: > >> I suspect that the fact that tempfiles are used by this module is a > >> private implementation detail, ie something that users are not supposed > >> to be aware of, or depend upon. > > Why, you can always check if the parameter you've got _is_ a Tempfile, > > and, if it is not, assume that it is small enough to be handled in core. > > > > Adopted from Samizdat source: > > > if Tempfile === file then > > File.syscopy(file.path, upload) > > else # StringIO > > File.open(upload, 'w') {|f| f.write(file.read) } > > end > > > Did I miss something? > > Why should I have to make that distinction in *my* code? I've got a Perl app > that I plan to port over to Ruby at some point, and it allows file uploads > (up to a couple megabytes in size). Indeed, the way that I deal with the > file uploads never touches the filesystem from the perspective of my > application (the file "string" goes directly into a MySQL database). If I > have to step into my own filehandling, then I may not port this app to Ruby > even though it's a fine candidate for it otherwise. > > The library should provide me the raw data. If I decide something is a file > and want to take some shortcuts with it, then the library can provide me > additional information (e.g., CGI.tempfile?(parameter_index)) and > functionality to access that implementation detail, but I should never > *have* to make the distinction in my application code. Ever. > > -austin > -- > austin ziegler * austin@halostatue.ca * Toronto, ON, Canada > software designer * pragmatic programmer * 2003.11.03 > * 09.35.55 > > >