Ex: In a form in an input field of file type in an html any user in my system appends a file. After pressing Submit I want the file to be kept in a JavaScript variable. Is there any kind of it? If not what do you advise me to do?
Ex: In a form in an input field of file type in an html any user in my system appends a file. After pressing Submit I want the file to be kept in a JavaScript variable. Is there any kind of it? If not what do you advise me to do?
You can do this with localStorage
using API file
.
Create a event handler for input file
and save it on localStorage
:
<input type="file" id="file">
<script>
var arq = document.body.querySelector("#file");
arq.addEventListener("change", function(){
localStorage.setItem("arquivo",this.files[0].name);
});
</script>
When you want to call the file name, use:
localStorage.getItem("arquivo");
If you only want to create a variable and assign it document.getElementById('input').files
(array of selected files), if you want only one file use .files[0]
JavaScript has the object File
, it has the data of the file (size, last modification date, name, type ...) and Blob
, in general this type can be used in any context of a% traditional%.