1

Note: The answers below reflect the state of legacy browsers in 2009.

Now you can actually do it. See the answer in this question:
How to set file input value programatically (i.e.: when drag-dropping files)?

I have two input field of type FILE. Is it possible that if user browses and chooses a file in one field ,that same file gets chosen in other field automatically.

<input type="file" name="file1" />
<input type="file" name="file2" />
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260

2 Answers2

1

You can not set file input using javascript due to security reasons. Please check How to set a value to a file input in HTML?.

  • I think this is different case.In my case,user already is giving input in form of one file.I just need to somehow copy that file. – Harshit Bansal Jul 27 '17 at 06:57
  • You cannot set the value to the input type file due to security reasons. please check with that url. I tried now. https://jsfiddle.net/sz66wtug/ @HarshitBansal – Sathish Jul 27 '17 at 06:59
  • You can clone the element and then change the name using jQuery but it is not a good practice to do this. – Sagar Jagodra Jul 27 '17 at 07:17
0

Maybe cloning an element works for you. But it seems like a dirty workaround.

function clone() {
  var input = document.getElementById("in");
  var divvi = document.getElementById("divvi");
  divvi.appendChild(input.cloneNode(true));
}
<div id="divvi">
  <input id="in" onchange="clone()" type="file" name="file1" />
</div>
Colin
  • 1,112
  • 1
  • 16
  • 27