开发者

rails form helper method: file_field?

开发者 https://www.devze.com 2023-03-27 19:45 出处:网络
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 c开发者_StackOverflowould I just get the file name

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 c开发者_StackOverflowould I just get the file name,I don't need the file itself.anything suggestion?


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


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", "");
  });
})
0

精彩评论

暂无评论...
验证码 换一张
取 消