简体   繁体   中英

Why is this code not alerting the text in the text file? (javascript/html)

Here is my code. The problem is that the information isn't being alerted. It worked in Js Fiddle but not on chrome/firefox/safari. When I tried to use document.write to display the text in JsFiddle that didn't work either. This code is giving me lots of problems.

<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
                    function readMultipleFiles(evt) {
            //Retrieve all the files from the FileList object
            var files = evt.target.files;
            if (files) {
                var r = new FileReader();
                for (var i = 0, f; f = files[i]; i++) {
                    r.onload = (function(f) {
                        return function(e) {
                            var contents = e.target.result;
                            alert(contents);
                        };
                    })(f);
                    r.readAsText(f);;
                }
            } else {
                alert("Failed to load files");
            }
        }
        document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);

    </script>
</head>
<body>
<input type="file" id="fileinput"/>

​</body>
</html>

try

window.onload = function() {
    document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);
}

Instead of just

document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);

Edit: So yeah, the solution was here

execute chrome with chrome.exe --disable-web-security for windows or google-chrome --disable-web-security for linux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM