Write data to <input type = "file"> using js
I am trying to assign the value <input type="file" id="field1"> <input type="file" id="field2"> .
I have the following code, but it does not work as expected:
<script type="text/javascript"> function test(){ var field_value = document.getElementById('file1').value; document.getElementById('file2').value = field_value; } </script> <body> <input type="file" onchange="test()" id="file1"/> <input type="file" id="file2"/> </body> Explanation: When I click on the field of view of a file that has id="file1" , it calls the test() function. The variable field_value has the name of the loaded file. But it does not assign a value to the file field id="file2" .
For security reasons, you cannot access file controls using Javascript (imagine if you can automatically download any file you want when a user visits a page!)
Having said that, the latest version of Firefox has a new File API that can help you in this particular browser. And there are various File Controls introduced in HTML5, but it will be a long time before you can use them.