FileReader.result return null
Asked Answered
B

2

7

I'm trying to setup a page that lets you upload a txt file (namely a log file that another program generates), and then manipulate the text further. So far I just want to console.log the text, but later on I'm planning to separate lines and get some specifics out of it to run my program.

Anyway, when I upload my log file I get the following: null

$("#draftlog").change(function() {
  var logFile = $('#draftlog').get(0).files[0];
  var reader = new FileReader;
  reader.readAsText(logFile);
  var rawLog = reader.result;
  console.log(rawLog);
});
body {
  margin: 0;
  min-width: 1200px;
}
.pickscontainer {
  margin-left: 5%;
  width: 90%;
  border: 1px solid black;
}
.cardbox {
  margin: 1px;
  border: 2px solid black;
  border-radius: 5px;
  width: auto;
  display: inline-block;
}
.card {
  width: 210px;
  height: 285px;
}
.picks {
  width: 90%;
  border: 1px solid black;
  height: 150px;
  margin-right: 5%;
  margin-left: 5%;
}
.inputbox {
  margin-left: 5%;
  width: 90%;
}
<html>

<head>
  <title>MTGO draft</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link type="text/css" rel="stylesheet" href="index.css">
</head>

<body>
  <div class="pickscontainer">
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
    <div class="cardbox">
      <img class="card" alt="card missing" src="" />
    </div>
  </div>
  <div class="inputbox">
    <input type="file" name="draftlog" id="draftlog">
  </div>
  <div class="picks"></div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="magic.js"></script>

</body>

</html>

There's the snippet. I've also uploaded a few of the log files I'm trying to read onto my server logs

Bartolommeo answered 22/2, 2015 at 13:32 Comment(5)
Where does the object utils get defined?Dexamyl
it doesn't as far as I can tell. I've used this example from File API (dev.w3.org/2006/webapi/FileAPI/#dfn-filereader) and it wasn't defined there. This is the first time I'm doing something like this, so I don't really know what's going on...Graminivorous
This is code for developers. Use MDN: developer.mozilla.org/en-US/docs/…Dexamyl
See my updated comment.Dexamyl
Updated question with the new code, now FileReader just returns null.Graminivorous
G
23

readAsText is asynchronous. You will need to wait until the .result is available, install a handler to do this:

var logFile = $('#draftlog').get(0).files[0];
var reader = new FileReader;
reader.readAsText(logFile);
reader.onload = function(e) {
    var rawLog = reader.result;
    console.log(rawLog);
};
Gliadin answered 22/2, 2015 at 14:11 Comment(2)
Assume we want to send for example 10 images using filereader at witch user can left some of them empty. Now what can we do?Prudhoe
@MohsenNemati You mean with 10 inputs? Just don't read those where input.files.length is 0.Gliadin
A
1
document.getElementById('previewImage').style.display = 'none';

function previewadminImage(input){
var filesSelected = document.getElementById('image').files[0];
     /*  console.log(filesSelected);  */  
    if(filesSelected){
        var reader1 = new FileReader();
        reader1.readAsDataURL(filesSelected);
        reader1.onload = function(){
            $("#previewImage").attr("src", reader1.result);
            document.getElementById('previewImage').style.display = 'block';
        }
      
    }
} 
Athanasius answered 8/6, 2021 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.