convert HTML file contents into a string in Javascript -


i'm asking users upload , html file, convert contents of html file string.

html file:

<form action="">   <input type="file" name="pic" accept="html" id = "htmlfile"> </form> 

javascript

function readtextfile(file) //this wrong think {     var rawfile = new xmlhttprequest();     rawfile.open("get", file, false);     rawfile.onreadystatechange = function ()     {         if(rawfile.readystate === 4)         {             if(rawfile.status === 200 || rawfile.status == 0)             {                 var alltext = rawfile.responsetext;                 alert(alltext);             }         }     }     rawfile.send(null); } 

if understand correctly, can read file after input change filereader this:

function readsinglefile(evt) {    //retrieve first (and only!) file filelist object    var f = evt.target.files[0];       if (f) {      var r = new filereader();      r.onload = function(e) {         var contents = e.target.result;        alert( "got file.n"               +"name: " + f.name + "n"              +"type: " + f.type + "n"              +"size: " + f.size + " bytesn"              + "contents:" + contents             );        }      r.readastext(f);    } else {       alert("failed load file");    }  }    document.getelementbyid('htmlfile').addeventlistener('change', readsinglefile, false);
<form action="">    <input type="file" name="pic" accept="html" id="htmlfile">  </form>

source


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -