簡體   English   中英

rails form helper 方法:file_field?

[英]rails form helper method: file_field?

你看到file_field提供了一個window讓用戶select某個文件上傳到服務器端。但我想要的只是文件名。我怎么能只得到文件名,我不需要文件本身。有什么建議嗎?

不知道你為什么需要這個,但你可以試試這個

以你的形式

<%= file_field :uploadfile %>

在你的 controller

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

“original_filename”將獲取正在上傳的文件的名稱,然后您可以將其存儲到數據庫中。 希望這可以幫助

你需要有兩個字段:

  • 存儲文件名的隱藏字段
  • 文件輸入選擇文件

前任:

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

小 jQuery 片段:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM