Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML 5 File upload event

I am trying to upload a file using the HTML File Api but I really seem not to get how to do it. What event do I have to give to the getImg() function?

HTML

<input id='img' type='file' onchange='getImg(event)'/>

JS

function getImg(evt){
 var files = evt.dataTransfer.files;
 var file = files[0];
console.log(file.name)
like image 898
Maximilian Lindsey Avatar asked Feb 23 '23 02:02

Maximilian Lindsey


1 Answers

The dataTransfer object is for drag&drop operations. Use the target instead.

<!DOCTYPE html>
<html>
    <body>
        <input id='img' type='file' onchange='getImg(event)'/>
        <script>
        function getImg(evt){
            var files = evt.target.files;
            var file = files[0];
            console.log(file.name);
        }
        </script>
    </body>
</html>
like image 121
Maurice Avatar answered Mar 05 '23 11:03

Maurice