I have a field input file
like the one in the image.
<td width="43%" colspan="3">Adjuntar un archivo:<input tabindex="-1" class="fileObligatorio" id="ControlAnexar0" style="width: 100%;" type="file">
<input type="button" onClick="location.reload()" value ="Reload">
After a location.reload()
page, the selected file is lost and you have to select it again.
Is there a way to save that file in a variable in Javascript to be able to load it again after a reload
?
I am afraid that for security reasons this is not possible.
As you say, for security reasons it is not possible to do what you want.
Here is more information about this browser behavior.
As Garrizan says in his answer , for security reasons you cannot keep the selected file in the
input type="file"
, but there are ways to keep the file when the page is reloaded.The idea would be to use the FileReader API to read the content of the file and store it in memory so that it can be used after the page is reloaded (you can see an example of how to read the content using FileReader in another answer I wrote on the topic ).
So what you should do is the following:
localStorage
(orsessionStorage
)localStorage
input type="file"
empty (in case the user wants to change the file).Here is an example of what the code might look like:
Note that this solution has some limitations:
localStorage
(or you could use some other method to store it),