Tutorial :Obtain filesize without using FileSystemObject in JavaScript



Question:

I am working on a project in ASP.NET, in which I want to get the filesize using JavaScript. Actually, I got the solution, and that is as like the following

<script language="javascript">      var Fo =new ActiveXObject("Scripting.FileSystemObject");      var fileName=new String();        function Trim(input)      {          var lre = /^\s*/;          var rre = /\s*$/;          input = input.replace(lre, "");          input = input.replace(rre, "");          return input;      }        function getSize(filePath)      {          try          {              var thefile = Fo.getFile(filePath);              var size = thefile.size;              return size;          }          catch(err){}      }        function getType(filePath)      {          try          {              var thefile = Fo.getFile(filePath);              var fileType = thefile.type;              return fileType;          }          catch(err){}      }        function ShowErrorPnl(tblPnl)      {          document.getElementById(tblPnl).style.visibility='visible';          document.getElementById(tblPnl).style.backgroundColor='Yellow';          document.getElementById(tblPnl).style.borderColor='Silver';      }        function UploadFile_CheckType(fileUploadCtrl,messageCtrl,hFieldCtrl,tblPnl)      {          try          {              var file = document.getElementById(fileUploadCtrl);              var fileName=file.value;              document.getElementById(messageCtrl).innerText='';              document.getElementById(tblPnl).style.visibility='hidden';              document.getElementById(hFieldCtrl).value='';   //  File can be uploaded.              //Checking for file browsed or not              if((getSize(fileName)/1024)>500)              {                  document.getElementById(messageCtrl).innerText='  File size is exceeding 500K';                    document.getElementById(hFieldCtrl).value='0';  // File cannot upload.                  ShowErrorPnl(tblPnl);                  file.focus();                  return false;              }              if(getType(fileName)!='Microsoft Office Word 97 - 2003 Document' &&                 getType(fileName)!='Adobe Acrobat Document')              {                  document.getElementById(messageCtrl).innerText=                    (document.getElementById(messageCtrl).innerText=='' ?                    '  Only Doc and pdf file can be upload!!!' :                    document.getElementById(messageCtrl).innerText +                      '\n'+'  Only Doc and pdf file can be upload!!!');                  ShowErrorPnl(tblPnl);                  document.getElementById(hFieldCtrl).value='0';  // File cannot upload.                  file.focus();                  return false;              }          }          catch(err){}      }  </script>  

It is working perfectly unless making some setting in Internet Explorer Properties.

In Internet Properties - > Select Security tab - > Local Internet - > Click on Custom Level button - > Initialize and script ActiveX controls not marked as safe for scripting - > Make it Enable

Save settings and execute program.

This is working, but I want to get filesize without using var Fo =new ActiveXObject("Scripting.FileSystemObject");


Solution:1

JavaScript on a Web page is not supposed to access the local filesystem. Allowing access would be a huge security hole. And you cannot expect your users to open up their system like this.

As a cumbersome workaround, make a form with a file control ("<input type="file">"), make the user choose the file, upload it to a server, let the server return file size.

In HTML5, you will be able to access the file that is chosen in the file control. Still no arbitrary filesystem access, but at least you can play with the files your user chose for you.

EDIT: to avoid a network roundtrip, you may ship a Java applet (not a JavaScript!) that implements a simplistic HTTP server for the sole purpose of file size calculation, then post the file to localhost and an arbitrary port that the applet's server listens on. A whole another can of worms will open though - Java VM, "Do you trust this applet or not?" windows, security software that block listening ports, etc.


Solution:2

For security reasons, you cannot interact with the local filesystem in JavaScript.

If you don't see why, imagine what would happen if any web page could read your My Documents folder.

Your question has no answer.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »