Using the HTML 5 Filesystem API to Uplode a File

Introduction

 
This is a simple article for beginners developing with HTML 5 that helps to understand how to upload a file in a browser using HTML 5 tools. We know that HTML is the client-side scripting language that helps display the data in a browser. We know that HTML is an acronym for "HyperText Markup Language" used to display data in a browser. HTML 5 is the advanced version of HTML. HTML5 will be the new standard for HTML, XHTML, and the HTML DOM. HTML 5 is the advanced version of HTML used to develop 3D or multimedia applications.
 
Step 1: First open an HTML editor such as Visual Studio.
  • Open start->Notepad.
  • The name of the editor is "Michael."
p.gif
 
Step 2:
Add a Folder on Desktop.
  • Right-click on desktop.
  • new->add folder.
p5.gif
 
Step 3: Open Visual Studio.
  • Go to file -> New->Projects.
  • Create an ASP. NET Web Application.
  • Name of "File.aspx".
p1.gif
 
p2.gif
 
Step 4: Add an HTML file to your web application.
  • Right-click of Solution Explorer.
  • Add->add new item->HTML form.
  • Name of HTML form is "File.html"
p4.gif
 
p6.gif
 
Step 5: Set the script browser a data on a File.
 
Code
  1. <script>  
  2. document.querySelector('input[type="file"]').addEventListener('change'function(e) {  
  3.   var files = this.files;  
  4.   loadInlineEffort('#fileListEffort'function(Effort) {  
  5.     Effort.onmessage = function(e) {  
  6.       console.log(e.data);  
  7.     };  
  8.     for (var i = 0, file; file = files[i]; ++i) {  
  9.       var CTX = new FileCTX();  
  10.       CTX.onload = function(e) {  
  11.         console.log(this.result);   
  12.       };  
  13.       CTX.onerror = function(e) {  
  14.         console.log(e);  
  15.       };  
  16.       CTX.readAsArrayBuffer(file);  
  17.     }  
  18.   };  
  19. }, false);  
  20.  function loadInlineEffort(selector, callback) {  
  21.  Win.URL =Win.URL ||Win.webkitURL || null;  
  22.  Win.BlobBuilder =Win.WebKitBlobBuilder ||  
  23.                    Win.MozBlobBuilder ||Win.BlobBuilder;  
  24.   var script = document.querySelector(selector);  
  25.   if (script.type === 'javascript/Effort') {  
  26.     var bb = new BlobBuilder();  
  27.     bb.append(script.textContent);  
  28.     callback(new Effort(window.URL.createObjectURL(bb.getBlob()));  
  29.   }  
  30. }  
  31. </script>  
Step 6: Set the functionality Fetching the entries of a File. 
 
code
  1. <script>  
  2. Win.resolveLocalFileSystemURL =  
  3. window.resolveLocalFileSystemURL ||  
  4. Win.webkitResolveLocalFileSystemURL;  
  5.     var ctx = new ctx('ctx.js');  
  6.     ctx.onmessage = function (e) {  
  7.         var ver = e.data.entries;  
  8.         ver.forEach(function (url, i) {  
  9.         Win.resolveLocalFileSystemURL(url, function (fileEntry) {  
  10.                 console.log(fileEntry.name);  
  11.             });  
  12.         });  
  13.     };  
  14.     ctx.postMessage({ 'cmd''list' });  
  15. </script>undefined</body>  
Step 7: Set a functionality to download the data on a browser.
 
Code
  1. <script>  
  2.     var ctx = new ctx('downloader.js');  
  3.     ctx.onmessage = function (e) {  
  4.         console.log(e.data);  
  5.     };  
  6.     ctx.postMessage({ fileName: 'CShorpCorner',  
  7.         url: 'cshorpcorner.png', type: 'image/png'  
  8.     });  
  9. </script>  
Step 8: The final code of the application is given below.
 
Code
  1. <html>  
  2.     <head>  
  3.         <title>Uplode a file using html 5</title>  
  4.         <script type="javascript/Effort" id="fileListEffort">  
  5.   self.onmessage = function(e) {  
  6.     postMessage(e.data);   
  7.   };  
  8.   </script>  
  9.     </head>  
  10.     <body>  
  11.         <input type="file" multiple>  
  12.             <script>  
  13. document.querySelector('input[type="file"]').addEventListener('change'function(e) {  
  14.   var files = this.files;  
  15.   loadInlineEffort('#fileListEffort'function(Effort) {  
  16.     Effort.onmessage = function(e) {  
  17.       console.log(e.data);  
  18.     };  
  19.     for (var i = 0, file; file = files[i]; ++i) {  
  20.       var CTX = new FileCTX();  
  21.       CTX.onload = function(e) {  
  22.         console.log(this.result);   
  23.       };  
  24.       CTX.onerror = function(e) {  
  25.         console.log(e);  
  26.       };  
  27.       CTX.readAsArrayBuffer(file);  
  28.     }  
  29.   });  
  30. }, false);  
  31. function loadInlineEffort(selector, callback) {  
  32.  Win.URL =Win.URL ||Win.webkitURL || null;  
  33.  Win.BlobBuilder =Win.WebKitBlobBuilder ||  
  34.                    Win.MozBlobBuilder ||Win.BlobBuilder;  
  35.   var script = document.querySelector(selector);  
  36.   if (script.type === 'javascript/Effort') {  
  37.     var bb = new BlobBuilder();  
  38.     bb.append(script.textContent);  
  39.     callback(new Effort(window.URL.createObjectURL(bb.getBlob()));  
  40.   }  
  41. }  
  42. </script>  
  43.         </body>  
  44.     </html>  
Step 9: Run an application in a browser.
 
Output