views:

417

answers:

1

Hi friend,

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 fetch those file name into new textbox below

please help to solve this problem.

Thanks

+1  A: 

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]
August Lilleaas
Thanks buddy i will look at this
Senthil Kumar Bhaskaran