Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting file upload contents using script

Is there a way to use JavaScript to set the file name and contents of a form's file uploads?

For example, suppose we have two file input elements.

<input type="file">
<input type="file">

Can JavaScript copy the content from one form element to the other?

like image 420
Eric Willigers Avatar asked Jul 01 '26 02:07

Eric Willigers


1 Answers

Add an onchange event on any of the input type file

Get its file property and assign to other input like

element2.files = element1.files;

var el = document.getElementById("1");
var el1 = document.getElementById("2");
el.onchange = function(){
el1.files = el.files;
 

}
<input type="file" id="1">
<input type="file" id="2">
like image 113
Muhammad Usman Avatar answered Jul 03 '26 16:07

Muhammad Usman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!