Restricting Gile Sizes during upload

Last post 08-02-2009, 10:42 PM by cutechat. 2 replies.
Sort Posts: Previous Next
  •  07-31-2009, 8:51 AM 54425

    Restricting Gile Sizes during upload

    Is there anyway to prevent a user from uploading a file based on filesize prior to the actual uploading of the files.
     I currently use:
            uploadSingleFileAttachments.ValidateOption.MaxSizeKB = maxUploadSizeBytes / 1024;
    To handle it but if a user uploads a 8mb file and it takes then 2 minutes to do so they are only told they wasted their time uploading it after the fact.
     
    is there a way to prevent them from uploading certain files at all?? 
     
     
     
  •  07-31-2009, 12:57 PM 54428 in reply to 54425

    Re: Restricting Gile Sizes during upload

    Yes. Please check this example:
     
    4. Simple Upload with Progress (Custom Validation)
    A sample demonstrates how to create user-defined validation functions for an upload control.

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  08-02-2009, 10:42 PM 54445 in reply to 54425

    Re: Restricting Gile Sizes during upload

    Hi,
     
    Please check this sample for script validation :
     
    1. <%@ Page Language="C#" Title="First sample" %>  
    2. <%@ Import Namespace="CuteWebUI" %>  
    3. <%@ Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" %>  
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">  
    5.   
    6. <script runat="server">  
    7.   
    8.     string disabledExtList = "aspx,asp,ashx,html,htm,mht,exe,dll,php,jsp";   
    9.   
    10.     void InsertMsg(string msg)   
    11.     {   
    12.         ListBoxEvents.Items.Insert(0, msg);   
    13.         ListBoxEvents.SelectedIndex = 0;   
    14.     }   
    15.     protected void UploadAttachments1_AttachmentAdded(object sender, AttachmentItemEventArgs args)   
    16.     {   
    17.         InsertMsg("Added.." + args.Item.FileName);   
    18.     }   
    19.   
    20.     protected void UploadAttachments1_FileValidating(object sender, UploaderEventArgs args)   
    21.     {   
    22.         //validate the extensions , this is very important!   
    23.         //the client side validation is not safe , double check it here:   
    24.         string ext=Path.GetExtension(args.FileName).TrimStart('.').ToLower();   
    25.         ext = "," + ext + ",";   
    26.         string list="," + disabledExtList.ToLower() + ",";   
    27.         if (list.IndexOf(ext) != -1)   
    28.         {   
    29.             throw (new Exception("Invalid file type!"));   
    30.         }   
    31.     }   
    32. </script>  
    33.   
    34. <html xmlns="http://www.w3.org/1999/xhtml">  
    35. <head id="Head1" runat="server">  
    36. </head>  
    37. <body>  
    38.     <form id="Form1" runat="server">  
    39.         <CuteWebUI:UploadAttachments runat="server" ID="UploadAttachments1" OnAttachmentAdded="UploadAttachments1_AttachmentAdded" OnFileValidating="UploadAttachments1_FileValidating">  
    40.         </CuteWebUI:UploadAttachments>  
    41.         <br />  
    42.         <div>  
    43.             Server Trace:   
    44.             <br />  
    45.             <asp:ListBox runat="server" ID="ListBoxEvents" Width="800"></asp:ListBox>  
    46.         </div>  
    47.     </form>  
    48.     <script type="text/javascript">  
    49.     var disabledExtList='<%=disabledExtList %>'  
    50.     </script>  
    51.     <script type="text/javascript">  
    52.     //validate the extensions in client side   
    53.     //this way is not safe , just for performance   
    54.     //try to disable it to test the server validation   
    55.     var useclientvalidation=true;   
    56.     function CuteWebUI_AjaxUploader_OnSelect(files)   
    57.     {   
    58.         if(useclientvalidation)   
    59.         {   
    60.             var list=","+disabledExtList+",";   
    61.             for(var i=0;i<files.length;i++)   
    62.             {   
    63.                 var size=files[i].FileSize;   
    64.                 var fps=files[i].FileName.split('.');   
    65.                 var ext=fps[fps.length-1].toLowerCase();   
    66.                 ext=","+ext+",";   
    67.                 if(list.indexOf(ext)!=-1)   
    68.                 {   
    69.                     alert("Javascript : Invalid file type : "+ext);   
    70.                     //cancel it.   
    71.                     return false;   
    72.                 }   
    73.             }   
    74.         }   
    75.     }   
    76.     </script>  
    77.   
    78. </body>  
    79. </html>  

     
    Regards,
    Terry
     
View as RSS news feed in XML