Senthil Kumar Bhaskaran
Senthil Kumar Bhaskaran

Reputation: 7451

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

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

Upvotes: 0

Views: 1208

Answers (1)

August Lilleaas
August Lilleaas

Reputation: 54593

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]

Upvotes: 2

Related Questions