Interacting With Files In HTML 5

HTML5 has a standardized File API that enables an HTML page to interact with local files on the browser file system. This API defines four key interfaces,

  • BLOB - This interface represents immutable raw data. A Blob has a type attribute that indicates the media type of the data, such as ‘text/plain’.
  • File - This interface inherits from Blob and represents an individual file.
  • FileList - This interface is a collection of File objects.
  • FileReader - This interface provides methods for reading a file or blob.

FileReader Interface

This interface provides three methods for reading data,

  • readAsText() - reads a file or blob and makes the contents available as plain text. This method is useful if you want to read the contents of a text file.
  • readAsDataURL() - reads a file or blob and makes the contents available as a data URL. This method is useful if you want to read the contents of a binary file, such as an image.
  • readAsArrayBuffer() - reads a file or blob and makes the contents available as an ArrayBuffer.

The FileReader object reads contents asynchronously and fires events to indicate the progress of the loading operation. Some of the events are as follows,

  • The progress event occurs repeatedly while data is being loaded, to indicate the progress.
  • The load event indicates that the data has been successfully loaded.
  • The abort event indicates that data loading has been aborted.
  • The error event indicates that an error occurred during loading.
  • The loadend event indicates that the read operation has completed, either successfully or unsuccessfully.

Let me show you an example of reading a text file. Following are the steps you are required to perform while reading a text file,

  • Get a File or Blob object, by <input type=”field”> or by drag-and-drop.
  • Create a FileReader object and handle events such as load and error.
  • Invoke readAsText() on the FileReader object.
  • In the load event handler function, access the text content in the result property of the event target.
  • In the error event handler function, implement appropriate error handling.

Exapmle

  1. <!doctype html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <title>Reading a File</title>  
  6.     <script type="text/javascript">  
  7.         function onLoadTextFile() {  
  8.             var myFile = document.getElementById("myTextFile");  
  9.             // Get the File object selected by the user, and be sure it is a text  
  10.             file  
  11.             if (myFile.files.length != 0 && myFile.files[0].type.match(/text.*/)) {  
  12.                 // Create a FileReader and handle teh onload and onerror events.  
  13.                 var reader = new FileReader();  
  14.                 reader.onload = function(e) {  
  15.                     var myDisplayContents = document.getElementById("theDisplayArea");  
  16.                     myDisplayContents.value = e.target.result;  
  17.                 };  
  18.                 reader.onerror = function(e) {  
  19.                     alert("Cannot load the file");  
  20.                 };  
  21.                 // Read text file, second parameter (encoding) is optional  
  22.                 reader.readAsText(myFile.files[0], "ISO-8859-1");  
  23.             } else {  
  24.                 alert("Select a text file");  
  25.             }  
  26.         }  
  27.     </script>  
  28. </head>  
  29.   
  30. <body>  
  31.     <h1> Reading a Text File </h1>  
  32.     <p> Select the text file: <input type="file" value="aa" id="myTextFile" onchange="onLoadTextFile()" />  
  33.         <hr>  
  34.         <h3>Contents of the File:</h3> <textarea id="theDisplayArea" rows="15" cols="40"> </textarea> </body>  
  35.   
  36. </html>  

Output 

Ebook Download
View all
Learn
View all