Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

javascript - FileReader.result return null

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...