Accessing an uploaded file without actually storing it in the database or on the server -- Ruby on Rails
I have a system where the 开发者_JAVA技巧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
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
- i.e.
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.
精彩评论