How to set a value to an input file using HTML ?

In HTML, we will use the type attribute to take input in a form and when we have to take the file as an input, the file value of the type attribute allows us to define an element for the file uploads. It displays a browse button on our computer screen, and when we click on this browse button, it asks the user for permission to select the file from his local computer.

Basic Syntax:

<input type="file">

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head></head>
  
<body>
    <form>
        <!--We tried to set the "file"
            value to attribute "type"-->
        <input type="file">
    </form>
</body>
  
</html>

chevron_right


Output:

But when we want to take file input by default, then we cannot do it. It means we cannot set a value to a file input due to some security reasons in HTML.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head></head>
  
<body>
    <form name="htmltest">
        <!--Here, by default we have tried
            to implement a file path using 
            the value attribute. But it 
            will not work here. -->
        <input type="file" value="c:/amrit.txt">
    </form>
</body>
  
</html>
<!--We can implement the submit 
    button using javascript. -->
<!--script>document.htmltest.submit();</script-->

chevron_right


The above code will give the same output as the previous code because here we want to set value, but it doesn’t work due to security reasons. Hence, in HTML, there is the only way to take file input.

full-stack-img




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.