Is there a type of Javascript that stores data from any file?

Asked

Viewed 59 times

-1

Ex: In a form in a file type input field in an html any user of my system attaches a file. After pressing Submit I wish the file to be kept in a Javascript variable. Is there any type capable of this? If not what I am recommended to do?

2 answers

-1

Yes just create a variable and assign it to 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 file data (size, last modification date, name, type...) and a Blob, in general this type can be used in any context of a Blob traditional.

Law more

  • If I want more than one file, I can use this . files[0] with another index?

  • of course, can make a loop to grab the data of all files

-2


You can do this with localStorage using the API file.

Create a Event Handler to the input file and save in 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");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.