繁体   English   中英

为什么此代码不警告文本文件中的文本? (javascript / html)

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

这是我的代码。 问题在于该信息未收到警报。 它在Js Fiddle中有效,但在chrome / firefox / safari中无效。 当我尝试使用document.write在JsFiddle中显示文本时,该文本也不起作用。 这段代码给了我很多问题。

<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>

尝试

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

不只是

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

编辑:是的,解决方案在这里

对于Windows,使用chrome.exe --disable-web-security执行chrome;对于Linux,则使用google-chrome --disable-web-security执行chrome。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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