0

I have a file input that gets the text from a file uploaded by a file input. It works fine with plain text but when I try and upload a RTF document it comes up with a load of garbled text.

I was wondering if I could convert the RTF document to a plain text file before displaying it on the screen.

Here is my code.

<html>
<body>
<input type="file" id="fileinput" />
<pre id="texts"></pre>
<script type="text/javascript">
  function readSingleFile(evt) {
    //Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0]; 

    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;
              document.getElementById("texts").innerHTML = contents;
      }
      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    }
  }

  document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
</script>
</body>
</html>

I don't mind about keeping images or styles, I just want to display the text.

Thanks in advance.

FinW
  • 111
  • 1
  • 6
  • My guess is you're not setting the encoding correctly, but really it could be anything. Can you post your code? – whatoncewaslost Mar 09 '16 at 19:53
  • Can you include example of `.rtf` file at Question ? See also https://github.com/jgm/pandoc – guest271314 Mar 09 '16 at 20:02
  • [Convert RTF to and from plain text](http://stackoverflow.com/questions/29922771/convert-rtf-to-and-from-plain-text) provides javascript code to convert RTF to text/plain using regular expressions. Written by the poster without claim of correctness in all cases. – traktor Mar 09 '16 at 23:47

0 Answers0