# FileReaderobject

  • The FileReader object allows a Web application to asynchronously read files stored on a user’s computer

Sample file content read

Upload file: <input type="file" name="file" id="fileId" onchange="read(event)"/>
</div>
<textarea id="fileTxt">Display text content</textarea>
  function read() {
    /** * Properties on file * lastModifiedDate: date when the file was last modified * name: file name * GBK Chinese character 2 bytes * UTF8 Chinese character 3 bytes * Size: file size * type: file type */
    var file = event.target.files[0];
    var reader = new FileReader();
    // Start reading the contents of the specified file. Once completed, the result attribute will contain a string representing the contents of the file that was read.
    reader.readAsText(file, "UTF-8");// Read the file
    reader.onload = function (evt) {
      var fileString = evt.target.result;
      document.getElementById('fileTxt').innerHTML = fileString; }}Copy the code

new FileReader()Brief introduction to examples

  • FileReader.onloadstartThe event
    • This event is fired when the read operation begins
  • FileReader.onloadThe event
    • This event is fired when the file read operation is complete.
  • FileReader.onloadendThe event
    • This event is triggered at the end of the read operation, whether it succeeds or fails
  • FileReader.readAsText(),FileReader.readAsBinaryString()andFileReader.readAsDataURL()methods
    • Read the specifiedBlobThe contents of,resultProperty will contain the contents of the file read.
      • readAsText()The result is displayed as a string
      • readAsBinaryString()It’s shown in raw binary
      • readAsDataURL()Is represented as a Base64 string
  • For other details, please refer toMDN 👉Click on the

# ActiveXObjectobject

FileReaderObjects are useful, but are only supported in IE10+, and can only be used for older versions of IEActiveXObject

  • Due to theActiveXObjectIs provided by Microsoft, so only support IE kernel browser, it can also be used to check whether the current browser is IE

Example for reading contents of an earlier VERSION IE file

<div> File path: <input type="input" id='URL' placeholder="Please enter the path of the file to be read." />
  <button onclick="getTxt()">To obtain</button>
 <textarea id="fileTxt">Display text content</textarea>
</div>
  function getTxt() {
    if (window.ActiveXObject || "ActiveXObject" in window) {return true;
	 }else{
                 alert('This browser does not support ActiveXObject')
	    	return false; 
	  }
    var ActiObj = new ActiveXObject("Scripting.FileSystemObject");
    var url=  document.getElementById('URL').value;
    var file = ActiObj.OpenTextFile(url, 1);// File path
    var txt = file.ReadAll();
    document.getElementById('fileTxt').innerHTML = txt;
  }
Copy the code
  • The full instance file is available for referenceGit 👉Click on the

Problems you might encounter

Display, Automation server could not create object

  • Such as:

  • This is due to security restrictions in Internet Explorer
  • Solution: CheckInternet explorer -> Internet Options -> Security -> Custom levelfindActiveX controlYou can modify it to enable it

  • Before there is an old device XP system installed IE8, thought ActiveXObject control problem, the results of the investigation is half a day indexOf does not support.