Rob Wilkerson
Rob Wilkerson

Reputation: 41236

RoR: Inspecting an Uploaded File

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 (https://www.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".

Upvotes: 0

Views: 3228

Answers (4)

Rob Wilkerson
Rob Wilkerson

Reputation: 41236

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.

Upvotes: 4

Ryan Bigg
Ryan Bigg

Reputation: 107718

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

Upvotes: 0

derfred
derfred

Reputation: 19831

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/

Upvotes: 0

Peter Krenn
Peter Krenn

Reputation: 397

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

Upvotes: 0

Related Questions