Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过FileReader()JavaScript获取文件内容_Javascript_Filereader - Fatal编程技术网

通过FileReader()JavaScript获取文件内容

通过FileReader()JavaScript获取文件内容,javascript,filereader,Javascript,Filereader,我正在尝试使用FileReader()和JavaScript获取文件内容 我发现这个答案: 正如@Markasoftware在评论中所说,我正试图将结果保存到var function readSingleFile(evt) { //Retrieve the first (and only!) File from the FileList object var myFile = evt.target.files[0]; var reader = ne

我正在尝试使用FileReader()和JavaScript获取文件内容

我发现这个答案:

正如@Markasoftware在评论中所说,我正试图将结果保存到var

function readSingleFile(evt) {
        //Retrieve the first (and only!) File from the FileList object
        var myFile = evt.target.files[0];
        var reader = new FileReader();
        var result;
        reader.readAsText(myFile);
        reader.onload=function(){ 
             result = reader.result; 
             console.log(result); // works
        }
        console.log(result); // not works
    }

若我试图在
onload
处理程序中查看内容,那个么一切都很好,但从中看不到结果。为什么?

因为onload是异步运行的<代码>控制台日志(结果);//在启动onload事件之前执行not works


更多信息:

示例

<html>
    <head>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
    </head>
    <body>
        <script>
            function PreviewImage() {
            var oFReader = new FileReader();
            oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);
            oFReader.onload = function (oFREvent) {
                var sizef = document.getElementById('uploadImage').files[0].size;
                document.getElementById("uploadPreview").src = oFREvent.target.result;
                document.getElementById("uploadImageValue").value = oFREvent.target.result; 
            };
        };
        jQuery(document).ready(function(){
            $('#viewSource').click(function ()
            {
                var imgUrl = $('#uploadImageValue').val();
                alert(imgUrl);
            });
        });
        </script>
        <div>
            <input type="hidden" id="uploadImageValue" name="uploadImageValue" value="" />
            <img id="uploadPreview" style="width: 150px; height: 150px;" /><br />
            <input id="uploadImage" style="width:120px" type="file" size="10" accept="image/jpeg,image/gif, image/png" name="myPhoto" onchange="PreviewImage();" />
        </div>
        <a href="#" id="viewSource">Source file</a>
    </body>
</html>

函数PreviewImage(){
var of reader=new FileReader();
readAsDataURL(document.getElementById(“uploadImage”).files[0]);
oFReader.onload=函数(OFRENT){
var sizef=document.getElementById('uploadImage')。文件[0]。大小;
document.getElementById(“uploadPreview”).src=ofretent.target.result;
document.getElementById(“uploadImageValue”).value=ofretent.target.result;
};
};
jQuery(文档).ready(函数(){
$('#viewSource')。单击(函数()
{
var imgUrl=$('#uploadImageValue').val();
警报(imgUrl);
});
});


谢谢。红色就行了。仍然不明白如何重新构造我的代码:(你能举个例子吗?这取决于你想实现什么。你需要
结果
作为另一个函数的输入吗?从事件处理程序中调用该函数,并将
结果
作为参数。是的。我必须用这个结果调用另一个函数,但我不想在读取文件后调用另一个函数。我不想o读取文件,显示输出,如果一切正常,则按下另一个按钮并调用另一个函数。然后,必须从事件处理程序调用显示输出的函数。然后,结果可以存储在全局变量中,并可以根据需要使用。请注意,按钮按下的事件处理程序也是异步的。