Validate Sum of file size for multiple files

Last post 06-28-2011, 8:54 AM by Eric. 5 replies.
Sort Posts: Previous Next
  •  06-22-2011, 7:30 PM 68094

    Validate Sum of file size for multiple files


    How can I validate the sum of the file size based on  multiple selected files?  I want to limit the total  disk space available for a user's files not the number of files. Can I do this and report it in the client validate dialog?
     
    Thanks
     
    Joe
  •  06-22-2011, 9:53 PM 68097 in reply to 68094

    Re: Validate Sum of file size for multiple files

    Hi FLJoe,
     
    Please try the example below
     
    1. <%@ Page Language="C#" %>  
    2.   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    4.   
    5.   
    6. <html xmlns="http://www.w3.org/1999/xhtml">  
    7. <head runat="server">  
    8.     <title>Untitled Page</title>  
    9. </head>  
    10. <body>  
    11.     <form id="form1" runat="server">  
    12.         <CuteWebUI:UploadAttachments ID="uploader1" runat="server" >  
    13.         </CuteWebUI:UploadAttachments>  
    14.     </form>  
    15. </body>  
    16. </html>  
    17. <script>  
    18. function CuteWebUI_AjaxUploader_OnSelect(files)  
    19. {  
    20.     //unit is b  
    21.     var maxLimit=1000000;  
    22.     //1000000=1mb  
    23.     var sum=0;  
    24.     for(var i=0;i<files.length;i++)  
    25.     {  
    26.         sum=sum+files[i].FileSize;  
    27.     }  
    28.     if(sum>maxLimit)  
    29.     {  
    30.         alert("Exceeded the maximum upload size limit");  
    31.         return false;  
    32.     }  
    33. }  
    34. </script> 
    Regards,
     
    ken
  •  06-23-2011, 9:26 AM 68119 in reply to 68097

    Re: Validate Sum of file size for multiple files


    Perfect! Thanks Ken.
     
    Is there also a server side event available?
     
    Joe
  •  06-23-2011, 10:59 AM 68121 in reply to 68119

    Re: Validate Sum of file size for multiple files

    Hi Joe,
     
    Please refer to the following snippet:

    <%@ Page Language="C#" Title="First sample" %> 
    <%@ Import Namespace="CuteWebUI" %> 
    <%@ Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> 
     
    <script runat="server"> 
     
        string disabledExtList = "aspx,asp,ashx,html,htm,mht,exe,dll,php,jsp";
        static int totalsize;

        protected void Uploader1_UploadCompleted(object sender, UploaderEventArgs[] args)
        {
            InsertMsg("the total size is " + totalsize+"bytes");         
        }
        void InsertMsg(string msg)  
        {  
            ListBoxEvents.Items.Insert(0, msg);  
            ListBoxEvents.SelectedIndex = 0;  
        }  
        protected void UploadAttachments1_AttachmentAdded(object sender, AttachmentItemEventArgs args)  
        {  
            InsertMsg("Added.." + args.Item.FileName);  
        }  
     
        protected void UploadAttachments1_FileValidating(object sender, UploaderEventArgs args)  
        {      
            totalsize += args.FileSize;
        }  
    </script> 
     
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head id="Head1" runat="server"> 
    </head> 
    <body> 
        <form id="Form1" runat="server"> 
            <CuteWebUI:UploadAttachments runat="server" ID="UploadAttachments1" OnUploadCompleted="Uploader1_UploadCompleted" OnAttachmentAdded="UploadAttachments1_AttachmentAdded" OnFileValidating="UploadAttachments1_FileValidating"> 
            </CuteWebUI:UploadAttachments> 
            <br /> 
            <div> 
                Server Trace:  
                <br /> 
                <asp:ListBox runat="server" ID="ListBoxEvents" Width="800"></asp:ListBox> 
            </div> 
        </form> 
        <script type="text/javascript">
            var disabledExtList = '<%=disabledExtList %>' 
        </script> 
        <script type="text/javascript">
            //validate the extensions in client side  
            //this way is not safe , just for performance  
            //try to disable it to test the server validation  
            var useclientvalidation = true;
            function CuteWebUI_AjaxUploader_OnSelect(files) {
                if (useclientvalidation) {
                    var list = "," + disabledExtList + ",";
                    for (var i = 0; i < files.length; i++) {
                        var fps = files[i].FileName.split('.');
                        var ext = fps[fps.length - 1].toLowerCase();
                        ext = "," + ext + ",";
                        if (list.indexOf(ext) != -1) {
                            alert("Javascript : Invalid file type : " + ext);
                            //cancel it.  
                            return false;
                        }
                    }
                }
            }  
        </script> 
     
    </body> 
    </html> 

    Thanks for asking
  •  06-24-2011, 10:16 AM 68138 in reply to 68121

    Re: Validate Sum of file size for multiple files


    Thanks Eric.
     
    In the file Validing server event, Is there a way to pass back something to indicate invalid and cancel?
     
    Joe
     
     
     
  •  06-28-2011, 8:54 AM 68199 in reply to 68138

    Re: Validate Sum of file size for multiple files

    Hi Joe,
     
    You can refer to the following method:
     
     protected void UploadAttachments1_FileValidating(object sender, UploaderEventArgs args)  
        {   
            string disabledExtList = "aspx,asp,ashx,html,htm,mht,exe,dll,php,jsp";        
            //validate the extensions  
            string ext=Path.GetExtension(args.FileName).TrimStart('.').ToLower();  
            ext = "," + ext + ",";  
            string list="," + disabledExtList.ToLower() + ",";  
            if (list.IndexOf(ext) != -1)  
            {  
                throw (new Exception("Invalid file type!"));  
            }  
        }  
     
    Thanks for asking
View as RSS news feed in XML