开发者

How to fetch File Field's file name into textbox using Rails and Prototype

开发者 https://www.devze.com 2022-12-08 04:01 出处:网络
I need to know how to fetch file field\'s file name into textbox using Rails and Prototype I need to upload any file using <%=file_field \"text\"%>

I need to know how to fetch file field's file name into textbox using Rails and Prototype

  1. I need to upload any file using <%=file_field "text"%>

  2. Then I need to fetc开发者_C百科h those file name into new textbox below


File fields are sandboxed in all modern browsers, so there are lots of things you can't do, such as setting their value. You can get the value, but you'll only get the name of the file, not the full path to the file.

Here's how you can do this with prototype, though.

<script type="text/javascript" charset="utf-8">
  Event.observe(window, "load", function(){
    $$("button")[0].observe("click", function(){
      $$("input[type=text]")[0].value = $$("input[type=file]")[0].value
    })
  })
</script>

<input type="file" />
<input type="text" />
<button>Test</button>

Here's the results I get on various browsers on my mac:

  • Opera: c:\fake_path[filename here]
  • Safari: [filename here]
  • Firefox: [filename here]
0

精彩评论

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