Javascript FileReader onload not firing

Cody B

I haven't used JavaScript in a while and I can't seem to read a text file and display the contents.

I've tried onload as well as onloadend. If I just put reader.onload = alert('Hello'); the alert fires, but I can't get anything to work with the function.

Not exactly sure where to go from here. I've tried defining the function after reader.onload = function(evt)... but that doesn't work.

I've tried in Safari 6.0.5 and Chrome as well.

<!DOCTYPE HTML>                                                                    
<html>                                                                             
<head>                                                                         
    <title>Pi to Colors</title>                                                
</head>                                                                        
<body>                                                                         
<script>                                                                       
function readFile() {                                                       
    var reader = new FileReader();                                             
    reader.onload = readSuccess;                                            
    function readSuccess(evt) {                                             
        var field = document.getElementById('main');                        
        field.innerHTML = evt.target.result;                                
    };                                                                      
    reader.readAsText("/pi.txt");                                              
}                                                                           
</script>                                                                      
<div id="main">                                                                

</div>                                                                         
</body>                                                                        
</html> 
LUKE

You can't grab a local file like that for security reasons.

Another underlying problem is that readAsText (and all the read functions) need the file's content and not its file path/name. You can grab this from the files collection of the input type="file" element. Here is how your code could work:

function readFile(file) {                                                       
    var reader = new FileReader();
    reader.onload = readSuccess;                                            
    function readSuccess(evt) { 
        var field = document.getElementById('main');                        
        field.innerHTML = evt.target.result;                                
    };
    reader.readAsText(file);                                              
} 

document.getElementById('selectedFile').onchange = function(e) {
    readFile(e.srcElement.files[0]);
};

Here is the jsfiddle: http://jsfiddle.net/fstreamz/ngXBV/1/

Note: this code not work in safari browser

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

FileReader() onload function not firing

From Dev

JavaScript onload function not firing

From Dev

JavaScript onload function not firing

From Dev

Javascript window.onload not firing

From Dev

Javascript filereader onload ( get file from server )

From Dev

Javascript onLoad File api event not firing

From Dev

return value calculated from javascript FileReader onload event

From Dev

JavaScript: Why FileReader.onload does not get called in mozilla?

From Dev

FileReader onload with result and parameter

From Dev

Delay the firing of the 'onload' event

From Dev

FileReader onload not within controller scope Angular 2

From Dev

How does FileReader.onload work?

From Dev

FileReader.onload can not return successful in time

From Dev

Canvas onload event isn't firing

From Dev

Outlook 2013 - VBA - Ribbon - onLoad not firing

From Dev

Explicitly Rendering ReCaptcha - Onload Function Not Firing

From Dev

IE8 iframe onload not firing

From Dev

jquery function not firing using onload event

From Dev

window.onload() is not firing in hybrid app

From Dev

Image.onload callback is firing inconsistently

From Dev

Javascript FileReader Multiple Images

From Dev

Javascript FileReader: Extracting text

From Dev

javascript FileReader get name?

From Dev

Javascript - Get the return of FileReader

From Dev

Javascript Filereader sort by name

From Dev

FileReader onload only works the second time around in Firefox?

From Dev

How do I use FileReader.onload multiple times on a page?

From Dev

Unable to get $scope variables inside the fileReader.onload function

From Dev

Change image with javascript onload