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
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 setcontent-typeof the form tomultipart/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.