How to get the filename from the Javascript FileReader? How to get the filename from the Javascript FileReader? javascript javascript

How to get the filename from the Javascript FileReader?


This is prob not the best solution, BUT it worked for me.

var reader = new FileReader();reader.fileName = file.name // file came from a input file element. file = el.files[0];reader.onload = function(readerEvt) {    console.log(readerEvt.target.fileName);};

Not the best answer, but a working one.


I just faced the same issue, here's how I fixed it:

Using FileReader

 const reader = new FileReader(); reader.readAsDataURL(event.target.files[0]); // event is from the HTML input console.log(event.target.files[0].name);


I got the filename and filesize through the FileReader this way

First of all, the reader is a javascript FILE API specification that is so useful to read files from disc.

In your example the file is readed by readAsDataURL.

reader.readAsDataURL(this.documentFile);var name = this.documentFile.name;var size = this.documentFile.size;

I tried on my site where use this.files[0] instead and worked fine to catch the name and the size with jQuery into an input element.

 reader.readAsDataURL(this.files[0]); $("#nombre").val(this.files[0].name); $("#tamano").val(this.files[0].size);