How to validate files while uploading?

Last post 01-18-2013, 5:17 PM by Kenneth. 3 replies.
Sort Posts: Previous Next
  •  01-17-2013, 3:10 PM 76680

    How to validate files while uploading?

    Hi,

     

    we have to validate files while uploading and if validation fails entire uploads should cancel

    And we should display error message to the user in a label control on the page.

    Could you help me what event we can use for this and how can we cancel the uploads if validation failed?

  •  01-18-2013, 1:10 PM 76688 in reply to 76680

    Re: How to validate files while uploading?

    Hi mvjr,

     

    You can validate the file in event "FileValidating". Do not need to cancel the upload by code, it will be changed automatically when throw error in this event.

     

    1. protected void Uploader1_FileValidating(object sender, UploaderEventArgs args)  
    2. {  
    3.     if (args.FileName == "abc.jpg")  
    4.     {  
    5.         throw (new Exception("not allow"));  
    6.      }  
    7. }  
     

    Regards,

     

    Ken 

     

  •  01-18-2013, 3:12 PM 76689 in reply to 76688

    Re: How to validate files while uploading?

    Hi Ken,

     

    Thanks for your reply.

     

    This event is throwing a javascript alert message and displaying a image file with a alert icon on the page.

     

    1. can we clear the file details if validation fails( marked the file details section in red in the below image )

    2.can we show the error message in a label instead of javascript alert?

     

     

     

  •  01-18-2013, 5:17 PM 76690 in reply to 76689

    Re: How to validate files while uploading?

    Hi mvjr,

     

    Please try the example below.

     

    1. <%@ Page Language="C#" %>  
    2.   
    3. <%@ Register Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" TagPrefix="CuteWebUI" %>  
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    5.   
    6. <script runat="server">  
    7.     protected void Uploader1_FileUploaded(object sender, UploaderEventArgs args)  
    8.     {  
    9.      if (args.FileName == "abc.jpg")  
    10.         {  
    11.             lb_error.Text = "upload error!";  
    12.             args.Delete();  
    13.         }  
    14.     }  
    15. </script>  
    16.   
    17. <html xmlns="http://www.w3.org/1999/xhtml">  
    18. <head id="Head1" runat="server">  
    19.     <title>example</title>  
    20. </head>  
    21. <body>  
    22.     <form id="form1" runat="server">  
    23.         <CuteWebUI:Uploader runat="server" ID="Uploader1"  OnFileUploaded="Uploader1_FileUploaded">  
    24.         </CuteWebUI:Uploader>  
    25.         <asp:Label ID="lb_error" runat="server"></asp:Label>  
    26.     </form>  
    27. </body>  
    28. </html>  
     

    Regards,

     

    Ken 

View as RSS news feed in XML