views:

25

answers:

1

I have a system where the user uploads a file and I have to read the file and display its contents on a form without storing it either on the server side or in the database

+1  A: 

When the file is uploaded Rails will automatically read it in and make it an instance of Tempfile so it's already stored, it won't however be stored forever on the system.

You can access the file using the normal params[:field_name] syntax as if the file were any other field (don't forget to set content-type of the form to multipart/form-data - form_for @mything, :html => {:multipart => true}) and you will get back the tempfile. The tempfile can be read from like any other file.

Rails (Or Maybe Rack I'm not 100% up to date) determines whether to do this or not to uploaded content based on the attachment part of the mulitpart/form-data element containing the file.

It might be possible to override things if you need to though to stop this storage from happening. Common practice however is to just work with the file and then let Ruby deal with the temp file.

Steve Smith
Usually Tempfile saves to the filesystem, but immediately deletes it so it doesn't show up in directory listings, and so it won't persist. Files are only scheduled for removal once the filehandle is closed, they still exist, so in this case it sticks around until you're done with it.
tadman