Extra validate

Last post 01-27-2011, 9:35 AM by SergeyC. 2 replies.
Sort Posts: Previous Next
  •  01-27-2011, 7:38 AM 65908

    Extra validate

    Can I add an extra (on server) check uploded files. And if necessary, interrupt the uploading process on server and show the message on client-side.

    Filed under:
  •  01-27-2011, 7:59 AM 65910 in reply to 65908

    Re: Extra validate

    Dear SergeyC,
     
    Yes, you can add extra code to validate the uploaded files and interrupt the upload process, please refer to the following code:
    <%@ 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 = "html,htm,mht,exe,dll";
      
        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)   
        {   
            //validate the extensions , this is very important!   
            //the client side validation is not safe , double check it here:   
            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!"));   
            }   
        }   
    </script>  
      
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head id="Head1" runat="server">  
    </head>  
    <body>  
        <form id="Form1" runat="server">  
            <script src="../PageUpload/TempSource.js"></script>  
            <CuteWebUI:UploadAttachments runat="server" ID="UploadAttachments1" 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 item = files[i];
                        var size = item.FileSize;
                        var fps = item.FileName.split('.');
                        var ext = fps[fps.length - 1].toLowerCase();
                        ext = "," + ext + ",";
                        if (list.indexOf(ext) != -1) {
                            item.Cancel();
                            alert("Javascript : Invalid file type : " + ext);
                        }
                    }
                }
            }   
        </script>  
      
    </body>  
    </html> 
     
    Thank you for asking
  •  01-27-2011, 9:35 AM 65915 in reply to 65910

    Re: Extra validate

    Thank you!

    And is it possible to make server validation before file total load?

View as RSS news feed in XML