locked
AjaxFileUpload Control hasFile Validation RRS feed

  • Question

  • User-1746098369 posted

    Dear Frens

    I am working with AjaxFileUpload Control , I need to validate the AjaxFileUpload Control on button submit to check wether it is containing the file or not

    I have checked , it is not having the .hasFile Property

    how to achieve it

    please help

    Monday, August 3, 2015 5:56 AM

Answers

  • User1724605321 posted

    Hi Rjk_P,

    , I need to validate the AjaxFileUpload Control on button submit to check wether it is containing the file or not

    It seems you want to validate the control whether has file from a external button , you could validate by jquery ,code below is for your reference:

      <script src="Scripts/jquery-2.1.4.min.js"></script>
        <script type="text/javascript">
            function validate() {
                if ($(".ajax__fileupload_fileItemInfo").length > 0) {
                    alert('file exist .');
                }
                else {
                    alert('select your file');
    
                }
            }
        </script>
    

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 4, 2015 2:17 AM

All replies

  • User1724605321 posted

    Hi Rjk_P,

    , I need to validate the AjaxFileUpload Control on button submit to check wether it is containing the file or not

    It seems you want to validate the control whether has file from a external button , you could validate by jquery ,code below is for your reference:

      <script src="Scripts/jquery-2.1.4.min.js"></script>
        <script type="text/javascript">
            function validate() {
                if ($(".ajax__fileupload_fileItemInfo").length > 0) {
                    alert('file exist .');
                }
                else {
                    alert('select your file');
    
                }
            }
        </script>
    

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 4, 2015 2:17 AM
  • User-1746098369 posted

    Thanks 

    you got it right , on an external Button I need to validate AjaxFilkeUpload for having the file or not 

    let me try it , thanks for your help 

    Tuesday, August 4, 2015 2:28 AM
  • User-1746098369 posted

     it worked for me , one more thing I am struggling with it .

    I have set the property to upload maximum file to 1 but it is per request property I have to limit the user to upload only one file 

    can you help me in this 

    Wednesday, August 5, 2015 1:11 AM
  • User1724605321 posted

    Hi Rjk_p,

    Rjk_P

    I have set the property to upload maximum file to 1 but it is per request property I have to limit the user to upload only one file 

    When setting MaximumNumberOfFiles="1" property to upload control , if i try to upload second file . it will warning: Maximum number of files exceeded  and cannot upload the second file , which has limited the user to upload only one file .

    For more information ,please refer to link below:

    http://mynewtec.blogspot.in/2012/08/add-common-caption-to-ajaxfileupload.html

    Best  Regards,

    Nan Yu

    Wednesday, August 5, 2015 1:21 AM
  • User-1746098369 posted

    hi Nan 

    I have checked it if I only select the File not upload it , it will warn 

    but if I browse it and upload it , it will allow me to upload another file , since the previous request is completed 

    I am also looking to disable the drag and drop feature of the Control

    Wednesday, August 5, 2015 2:47 AM
  • User-1746098369 posted

    hi NAN

    My Requiredfieldvalidators are bypassed if i am using this script 

    Thursday, August 6, 2015 1:07 AM
  • User1724605321 posted

    Hi Rjk_P ,

    I suggest you could you could use F12 Developer Tools to check why make the code bypassed and show me your code about your validate function .

    Best Regards,

    Nan Yu

    Thursday, August 6, 2015 3:27 AM
  • User-1746098369 posted

    Hi Nan 

    thanks for your reply,  I got it resolved I have added the following Page_ISValid in the script 

    <script type="text/javascript">
    function validate() {
    if (!Page_IsValid) {
    return false;
    }
    if ($(".ajax__fileupload_fileItemInfo").length > 1) {
    alert('only 1 File is allowed to Upload');
    }
    else {
    alert('Select thesis file (only .pdf) to upload it');
    }
    }
    </script>

    Thursday, August 6, 2015 4:16 AM
  • User-1746098369 posted

    Nan

     can you help me in one more thing , I am using the following code to check the valid .df file , it is executing but not able to alert to the user that it is not a valid pdf file 

    and AjaxFileUpload control is showing "File Uploaded" which is wrong 

    how to handle it , can you please help in it 

     Protected Sub AjaxFileUpload1_OnUploadComplete(ByVal sender As Object, ByVal file As AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
            ' User can save file to File System, database or in session state
            ' Validation added to check the taken file is a real pdf file or not 
            Dim r As New System.IO.BinaryReader(file.GetStreamContents)
            Dim fileclass As String = String.Empty
            Dim buffer As Byte
    
            buffer = r.ReadByte()
            fileclass = buffer.ToString
            buffer = r.ReadByte
            fileclass &= buffer.ToString
            If (fileclass = "3780") Then
                bool1 = True
            Else
                bool1 = False
            End If
            If bool1 = False Then
                Label1.Text = "please Choose a Valid pdf File"
                Exit Sub
            End If
            If Not r Is Nothing Then
                r.Dispose()
            End If
    end sub

    Thursday, August 6, 2015 4:19 AM
  • User1724605321 posted

    Hi  Rjk_P,

    Please try to set AllowedFileTypes property for AjaxFileUpload:

    <ajaxtoolkit:ajaxfileupload id="AjaxFileUpload1" xmlns:ajaxtoolkit="#unknown">
        ThrobberID="myThrobber"
        ContextKeys="fred"
        AllowedFileTypes="jpg,pdf"
        MaximumNumberOfFiles=10
        runat="server"/></ajaxtoolkit:ajaxfileupload>

    You could also refer to links below for AjaxFileUpload Control and its Validation:

    http://forums.asp.net/t/1801968.aspx?AjaxFileUpload+and+AllowedFileTypes

    Best Regards,

    Nan Yu

    Thursday, August 6, 2015 4:28 AM
  • User-1746098369 posted

    Hi Nan 

    thanks for your reply , I have already set the property to .pdf, but the scenario is user is changing the extenstion of jpeg file to pdf 

    e.g. 1.jpeg.pdf 

    I have to restrict this for which I am using the above code 

    Thursday, August 6, 2015 4:34 AM
  • User-1746098369 posted

    now validation are ok , but it is showing only the alert and then postback occuered and data saved without file :(

    Thursday, August 6, 2015 4:45 AM
  • User-1746098369 posted
    Hi Rjk_P ,
    
    I suggest you could you could use F12 Developer Tools to check why make the code bypassed and show me your code about your validate function .
    
    Best Regards,
    
    Nan Yu

    this is not working , can you help 

    <script type="text/javascript">
            function validate() {
                if (!Page_IsValid) {
                    return false;
                }
                else {
                    if ($(".ajax__fileupload_fileItemInfo").length > 0) {
                        alert('only 1 File is allowed to Upload');
    
                    }
                    else {
                        alert('Select thesis file (only .pdf) to upload it');
                    }
                }
            }
    
        </script>
     <asp:Button ID="btnSubmit" runat="server" Text="Submit your Thesis " 
                           Visible="False" OnClientClick="validate();" ValidationGroup="submit"   />



    Thursday, August 6, 2015 5:00 AM