write data into <input type="file"> using js
I'm trying to assign the value of <input type="file" id="field1">
to <input type="file" id="field2">
.
I'm having the following code but it is not working as expected:
<script type="text/javascript">
function test(){
var field_value = document.getElementById('file1'开发者_StackOverflow).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 browse field of file field having id="file1"
it calls test()
function. Variable field_value
is having the name of the file uploaded. But it is not assigning the value to file field having id="file2"
.
For security reasons you cannot access the file controls with Javascript (imagine if you could automatically upload any file you want when the user visits a page!)
Having said that, the newest version of Firefox has a new File API, which might help you for that specific browser. And there are various File controls introduced in HTML5, but it will be a long time before you can use them.
You cannot get nor set the value of an <input type="file" >
in Javascript.
精彩评论