Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a value to a file input in HTML?

How can I set the value of this?

<input type="file" /> 
like image 426
Alon Gubkin Avatar asked Nov 08 '09 15:11

Alon Gubkin


People also ask

How do you assign a value to a file in HTML?

The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.

What is the value of input type file?

A file input's value attribute contains a string that represents the path to the selected file(s). If no file is selected yet, the value is an empty string ( "" ). When the user selected multiple files, the value represents the first file in the list of files they selected.


2 Answers

You cannot, 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? =)

like image 77
BalusC Avatar answered Sep 28 '22 06:09

BalusC


You can't.

The only way to set the value of a file input is by the user to select a file.

This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.

like image 25
Guffa Avatar answered Sep 28 '22 06:09

Guffa