castiel
castiel

Reputation: 2783

rails form helper method: file_field?

you see the file_field provide a window to let the user select a certain file and upload to the server side.But what I want is just the file name.How could I just get the file name,I don't need the file itself.anything suggestion?

Upvotes: 2

Views: 3270

Answers (2)

fl00r
fl00r

Reputation: 83680

you need to have two fields:

  • hidden field to store filename
  • file input to choose file

Ex:

<%= file_field_tag :our_file %>
<%= f.hidden :filename, :id => "hidden_filename" %>

little jQuery snippet:

$(document).ready(function(){
  $('input[type=file]').change(function(e){
    filename = $(this).val();
    $("#hidden_filename").attr("value", filename);
    # To reset file field if you don't want to uppload a file
    $(this).attr("value", "");
  });
})

Upvotes: 3

Hishalv
Hishalv

Reputation: 3052

not sure why you would need this, but you can try this

in your form

<%= file_field :uploadfile %>

and in your controller

def upload
    params[:uploadfile].original_filename
    .... process the rest of this method ....
end

The "original_filename" will get the name of the file that is being uploaded, then you can store into the database. hope this helps

Upvotes: 5

Related Questions