Shyam
Shyam

Reputation: 2377

Rails: form input type and getting the filename

As I am using Ruby on Rails to build an application, which only runs locally, I am lost in the woods (a nuby without a compass). I have a simple MVC application and my view is missing one thing I could really use. I want to select a local file just to retrieve it's filename. I know it's relatively easy to use the form tag helpers for uploading:

<%= file_field 'upload', 'datafile' %></p>

I wonder how I could get the filename from the selected file, without uploading the file.

Upvotes: 1

Views: 486

Answers (2)

VP.
VP.

Reputation: 5141

You could use jquery. Just do something like

var name = $('my_input_id').val();
alert(name);

if you have to pass the name to your application you could just use jquery post to your controller. Your controller should be able to understand the format js (hint: respond_to).

Upvotes: 1

Toby Hede
Toby Hede

Reputation: 37133

Not sure if you can do this without uploading the file.

If the form isn't set to multipart, the file won't be uploaded, just not sure what data is sent. But ... have a look in your logs and see what parameters are being passed with the request ... you might see the file name as a string.

Upvotes: 0

Related Questions