Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to check file size before upload using Fileupload control in asp .net using javascript or jquery.
Posted

Dear Friend,

Checking a FileSize through with javascript may impose serious issues on the browsers who don't allow javascript to run. Also, javascript is using ActiveX object and nowadays in latest browsers it will ask for user permission to access ActiveX object. Sometimes it may happen that browser does not support ActiveX object or it is disabled so the code in jQuery or javascript fails.

So for if you are using ActiveX object and knows that browser will defiantly support the below script then you are through with this problem:-

<script language="javascript" type="text/javascript">
    var objFSO ;
    try
    {
        objFSO = new ActiveXObject("Scripting.FileSystemObject");
    }
    catch(err)
    {
        alert(err.message);
        alert(err.number);
    }
    function CheckExtention(el) // pass Upload File object’s ClientID here
    {
        var myel= document.getElementById(el);

        var file;

        var path = myel.value;

        //alert(path);

        file = objFSO.getFile(path);

        var size;

        size = file.size ; // This size will be in Bytes

  // We are converting it to KB as below

        alert(‘File Size is : ‘ + file.size /1024 +‘ KB’);
    }
</script>


I hope you understand.

Thanks
 
Share this answer
 
Comments
Member 9520858 17-Oct-12 4:55am    
i've tried it, but "myel.value" give me a fake path.. how can i fix it?
You can't, not for any file type - Javascript is not allowed near the users file system for security reasons. There are a number of specific tricks you can use which can work, but they tend to be browser specific (or even version specific for IE) and image-only related.

You could use an ActiveX control, but the user may not accept them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900