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.5k16 gold badges58 silver badges86 bronze badges
asked Apr 30, 2016 at 14:17
Andrew O Neill
711 silver badge6 bronze badges
1 Answer 1
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"/>
answered Apr 30, 2016 at 14:53
Mohammad
21.5k16 gold badges58 silver badges86 bronze badges
Sign up to request clarification or add additional context in comments.
8 Comments
Andrew O Neill
Yeahthat is what I have but how do I get the contents of the file I choose other than its name
Mohammad
All content of file is in object that named files. see your console after selecting file.
Andrew O Neill
where in the console will I see the text that is in the file? for instance am I able call files[0].text?
Mohammad
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.
Andrew O Neill
Yes I see the file in the console, how do I get the text from the file I have selected?
|
Explore related questions
See similar questions with these tags.
default