Ad

Javascript Filereader Only Allow .log Files?

I have an ASP.NET MVC Application where I can choose a .log File. This happens with a FileReader. But when the explorer opens, all kind of datas are displayed (.png, .jpg, .docx....). So I want, that only the .log Files are displayed.

Here is my code:

// Input File (BUTTON)
const input = document.querySelector('input[type="file"]');

//Get data input from user
input.addEventListener('change', function (e) {

    $('#fileName').html(input.files[0].name);

    const reader = new FileReader()
    reader.onload = function () {
        const reslt = reader.result;
        readFile(reslt);
    }
    reader.readAsText(input.files[0])
    document.getElementById('rankings').style.display = 'none';
}, false)
Ad

Answer

This question relates more to html than to javascript. You should use the accept attribute and list a comma separated extensions accepted.

In your case:

<input type="file" accept=".log" />
Ad
source: stackoverflow.com
Ad