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?

Mohammad
  • 21,175
  • 15
  • 55
  • 84

1 Answers1

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"/>
Mohammad
  • 21,175
  • 15
  • 55
  • 84