2

I am trying to get file contents with FileReader(), JavaScript.

I find this answer: https://stackoverflow.com/a/21962101/2898694

As @Markasoftware said in comments, I am trying to save result to 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
    }

If I am trying to see content in onload handler all fine, but out of it I can't see result. Why?

2 Answers 2

0

Because onload runs asynchrone. console.log(result); // not works is executed before the onload event has fired.

More on that: How do I return the response from an asynchronous call?

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Red it just. Still don't understand how to restructure my code :( Can you give an example?
It depends on what you want to achieve. You need the result as input to another function? Call the function from within the event handler with result as parameter.
Yes. I have to call another functions with this result, but I don't want to call another function, just after file is readed. I want to read file, show output, if all fine, press another button and call another function.
Then your function which shows the output has to be called from the event handler. The result can then be stored in a global variable and be used as you like. Notice that the event handler for the button press also is asynchron.
0

example

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.