Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to DOM using just JavaScript [duplicate]

How can I set the value of this?

<input type="file" />
like image 435
Alon Gubkin Avatar asked Mar 07 '26 08:03

Alon Gubkin


1 Answers

You cannot set it to a client side disk file system path, due to security reasons.

Imagine:

<form name="foo" method="post" enctype="multipart/form-data">
    <input type="file" value="c:/passwords.txt">
</form>
<script>document.foo.submit();</script>

You don't want the websites you visit to be able to do this, do you? =)

You can only set it to a publicly accessible web resource as seen in this answer, but this is clearly not the same as a client side disk file system path and it's therefore useless in that context.

like image 94
BalusC Avatar answered Mar 08 '26 22:03

BalusC