views:

266

answers:

4

I need to inspect the bits of an uploaded file before it's ever saved off to the file system. PHP's documentation has a nice page that tells me exactly what properties are available for me to use (http://us3.php.net/manual/en/features.file-upload.post-method.php), but I can't find something similar for Ruby and/or Rails.

I've also tried logging a JSON-formatted string of the upload, but that just gives me a redundant UTF-8 error. I can't think of anything else to try.

Can anyone offer any insight or point me to the right place?

Thanks.

UPDATE: I'm running Apache 2.2.11 on OS X (Leopard) in case Peter is right (see below).

UPDATE: In case it helps, my input parameter is logged as "upload"=>#<File:/tmp/RackMultipart.64239.1>. I'm just not sure how to access it to get to its "parts".

A: 

I think this depends on the web server you're using. I remember having different fields for mongrel, apache and nginx.

Peter Krenn
Ugh. I'll update the original question accordingly. I'm surprised at how little info I can find for this or about any way to reliably dump the upload.
Rob Wilkerson
A: 

AFAIK Rails and the various app servers totally abstract the upload part. However here is a thorough discussion of the topic: http://www.jedi.be/blog/2009/04/10/rails-and-large-large-file-uploads-looking-at-the-alternatives/

derfred
A: 

This is just a File object, something that you can duplicate by going:

File.open("some_file")

The /tmp/RackMultipart.64239.1 is just a filename.

If you want to see/output its contents from the controller:

 puts params[:upload].read
Ryan Bigg
I guess I assumed that the filename represented a physical file that I could inspect before ever saving it to a permanent location. That doesn't appear to be the case. There are properties like content_type and original_filename that can be gleaned from the upload, but I can't find a complete set of which properties are available at that time.
Rob Wilkerson
+1  A: 

As far as I've been able to tell or find, there is no physical file until an upload is read. This is inline with derfred's reply. The only metadata that can be accessed is:

uploaded_file.content_type  # the uploaded file's MIME type
uploaded_file.original_path # which is really just the name of the file

Additionally, there's a read method on the uploaded_file that allows the file' content to be accessed and, presumably, written to the permanent file system.

Something else that I've noticed is that the only means Ruby offers to inspect the MIME type of a file is in this content_type property of an uploaded file. Once the file is on the file system, there's no longer any way of accessing or determining the MIME type.

Rob Wilkerson