views:

25

answers:

1

Uploading a file in PHP results in a map. One of the keys, tmp_name, allows the user to manipulate the file without having to move it around. I'd like to do the same for Ruby on Rails.

I have a form that takes a .csv file. With this, I'm trying to populate a database. However using just the file field from the form, I get a type of ActionController::UploadedFile which doesn't allow me to do CSV.open or iterate over it. So how can I access the file uploaded without moving it into my project and then iterate over it? Thanks.

+1  A: 

The method ActionController::UploadedFile#path should return the path of the tmp file on the server. You also have a handy #read method that returns the content of the file.

file_param = params[:upload][:file]
filename = file_param.original_filename
filename = file_param.path
filedata = file_param.read

CSV.open(file_param.path, "r") { ... }
Simone Carletti
I'm using rails 2.0.2, complying with the professor's use of instantrails which is stuck at that version as well. Unfortunately the path method wasn't available, however I was able to use the read method and figured it out from there, thanks!
tipu