Mandar
Mandar

Reputation: 317

How to pass the name of a file to Ruby controller when uploading it?

I am new to ruby on rails. I am trying to upload a CSV file in my application, parse it and save the contents in database. I want to pass the name of the csv file when I am uploading it to the controller. I am not sure how to pass the actual name of the CSV file to my Ruby Controller.

Code ex:

view :
<h1>File Upload</h1>
<%== form_tag({:action=>'uploadFile'}, :multipart => true) %>
<input type="file" name="csvfile" /><br />
<input type="submit" name="Upload" value="Upload") %>" />
</form>

controller:
class UploadController < ApplicationController
def index
    render :file => 'app\views\upload\uploadfile.html.erb'
end
def uploadFile
    csvfile = params[:csvfile]
    render :text => "File has been uploaded successfully"
end
end

Upvotes: 0

Views: 178

Answers (1)

Babur Ussenakunov
Babur Ussenakunov

Reputation: 1995

Uploaded file is an instance of ActionDispatch::Http::UploadedFile. It has original_filename attribute:

def uploadFile
  filename = params[:csvfile].original_filename
  # ...
end

Upvotes: 1

Related Questions