Experimenter
Experimenter

Reputation: 167

Display array (read and processed from text file) elements to new lines in JavaScript

I've below code where user can upload text file. I tried to display output in div after splitting it using @ as split character. Array elements stored into variables are displayed correctly with new line in alert but are printed in a single line in div.

  function readBlob(opt_startByte, opt_stopByte) {

    var files = document.getElementById('files').files;
    if (!files.length) {
      alert('Please select a file!');
      return;
    }
    var file = files[0];
    var start = parseInt(opt_startByte) || 0;
    var stop = parseInt(opt_stopByte) || file.size - 1;
    var reader = new FileReader();

    // If we use onloadend, we need to check the readyState.
    reader.onloadend = function(evt) {
      if (evt.target.readyState == FileReader.DONE) { // DONE == 2
	  	var test = evt.target.result;
		var htmlstring = test.split('@');
		var data = htmlstring.join('\r\n');
        document.getElementById('byte_content').textContent = data;
		alert(data); //with line-break
		alert(htmlstring); // without line-break
      }
    };
    var blob = file.slice(start, stop + 1);
    reader.readAsBinaryString(blob);
  }
  document.querySelector('.readBytesButtons').addEventListener('click', function(evt) {
    if (evt.target.tagName.toLowerCase() == 'button') {
      var startByte = evt.target.getAttribute('data-startbyte');
      var endByte = evt.target.getAttribute('data-endbyte');
      readBlob(startByte, endByte);
    }
  }, false);
 
  #byte_content {
    margin: 5px 0;
    max-height: 100px;
    overflow-y: auto;
    overflow-x: hidden;
  }
  #byte_range { margin-top: 5px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="files" name="file" /> Read bytes: 
<span class="readBytesButtons">
  <button>entire file</button>
</span>
<div id="byte_content"></div>

Text file data:

this is some text @2135311 164 @df fdg 

Current output:

this is some text 2135311 164 df fdg 

Desired output:

this is some text 
2135311 164 
df fdg 

Upvotes: 1

Views: 108

Answers (2)

zer00ne
zer00ne

Reputation: 43880

You need to render data in HTML not plain text:
Instead of \r\n use <br>.
Instead of .textContent use innerHTML

var data = htmlstring.join('<br>');
document.getElementById('byte_content').innerHTML = data;

Demo

function readBlob(opt_startByte, opt_stopByte) {

  var files = document.getElementById('files').files;
  if (files.length < 1) {
    alert('Please select a file!');
    return;
  }
  var file = files[0];
  var start = parseInt(opt_startByte) || 0;
  var stop = parseInt(opt_stopByte) || file.size - 1;
  var reader = new FileReader();

  // If we use onloadend, we need to check the readyState.
  reader.onloadend = function(evt) {
    if (evt.target.readyState == FileReader.DONE) { // DONE == 2
      var test = evt.target.result;
      var htmlstring = test.split('@');
      var data = htmlstring.join('<br>');
      document.getElementById('byte_content').innerHTML = data;
      alert(data); //with line-break
      alert(htmlstring); // without line-break
    }
  };
  var blob = file.slice(start, stop + 1);
  reader.readAsBinaryString(blob);
}
document.querySelector('.readBytesButtons').addEventListener('click', function(evt) {
  if (evt.target.tagName.toLowerCase() == 'button') {
    var startByte = evt.target.getAttribute('data-startbyte');
    var endByte = evt.target.getAttribute('data-endbyte');
    readBlob(startByte, endByte);
  }
}, false);
#byte_content {
  margin: 5px 0;
  max-height: 100px;
  overflow-y: scroll;
  overflow-x: hidden;
}

#byte_range {
  margin-top: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="file" id="files" name="file" /> Read bytes:
<span class="readBytesButtons">
  <button>entire file</button>
</span>
<div id="byte_content"></div>

Upvotes: 1

Experimenter
Experimenter

Reputation: 167

Just replacing div with textarea tag to display content it's working fine.

Upvotes: 0

Related Questions