1

I need to create a file upload field which I have below:

<div class="col-xs-4 col-md-4 form-group">
    <label for="file-upload" >File Upload</label>
    <input type="file" id="file-upload"/>
</div>

How do I access the contents of the selected file without going to another page or adding anything to the URL in Javascript?

0

1 Answer 1

3

Use this simple code

var inputFile = document.getElementById("file-upload");

inputFile.addEventListener("change", function(){
    var files = inputFile.files;
    console.log(files[0]);
    alert(files[0].name);
});
<input type="file" id="file-upload"/>

Sign up to request clarification or add additional context in comments.

8 Comments

Yeahthat is what I have but how do I get the contents of the file I choose other than its name
All content of file is in object that named files. see your console after selecting file.
where in the console will I see the text that is in the file? for instance am I able call files[0].text?
After selecting file, content of selected file writed in console. To see browser console press F12 key then click on console tab on top of opened panel.
Yes I see the file in the console, how do I get the text from the file I have selected?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.