0

I am trying to let a user select a text file (via a standard file dialog) in a Wakanda web app and read it into a variable. Is there a built-in Wakanda client side call to do this or a jquery way?

1 Answers1

2

You can do it with pure HTML.

Example 1

The simple file selector is like this:

<input type=file name=varname>

Example 2

If you actually want to read the file on the client side into a variable then the HTML5 FileReader should help:

document.getElementById('file').addEventListener('change', readFile, false);

function readFile (evt) {
  var files = evt.target.files;
  var file = files[0];           
  var reader = new FileReader();
  reader.onload = function() {
    var output = this.result;
    document.getElementById('test').innerHTML = output;
  }
  reader.readAsText(file)
}
<input type="file" id="file" name="file" enctype="multipart/form-data" />
<br />
<br />output:<br />
<span id=test></span>

Example 2 is a slightly modified version of this answer

Community
  • 1
  • 1
Tim Penner
  • 3,551
  • 21
  • 36