locked
AjaxFileUpload OnUploadComplete Event issue RRS feed

  • Question

  • User-1746098369 posted

    Dear Friends 

    I need to validate the pdf file , for is it a actual pdf file or not ? 

    I am using this code on AjaxFileUpload onUploadComplete event like this 

    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

    the problem is , the code is running but I need to alert to user for valid pdf file but the label is not showing although it is oging in that block 

    and the Control is showing fileuploaded successfully even the file is not uploaded 

    how to handle it please help 

    Thursday, August 6, 2015 2:03 AM

Answers

  • User-271186128 posted

    Hi RJK,

    user can change the extension of .aspx page to .pdf like 1.aspx.pdf and can be able to upload it 

    As for this issue, I suggest you could check the filename, whether it contains another extension, such as (.aspx, .txt). I suggest you could try to use Asp.Net File upload control, then when click the submit button, you could get the filename and validate it using JavaScript or JQuery. Here is a sample, you could refer to it:

            <div>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:RequiredFieldValidator ErrorMessage="Required" ControlToValidate="FileUpload1"
                    runat="server" Display="Dynamic" ForeColor="Red" />
                <span id="lblError" style="color: red;"></span>
                <asp:Button ID="btnSubmit" runat="server" Text="Upload" OnClientClick="return MyFunction();" />
            </div>

    JavaScript code:

        <script type="text/javascript">
            function MyFunction() {
                var fileUpload = document.getElementById("FileUpload1");
                var lblError = document.getElementById("lblError");
    
                var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(.pdf)$");
                
                var denyFiles = [".doc", ".docx", ".aspx"];
                var regex2 = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + denyFiles.join('|') + ")+(.pdf)$");
    //Check whether it contains multiple extension. if (regex2.test(fileUpload.value.toLowerCase())) { lblError.innerHTML = "Please check the upload files, the extensions not right."; return false; } else if (!regex.test(fileUpload.value.toLowerCase())) { //Check whether it is a pdf file. lblError.innerHTML = "Please upload files having extensions: <b>pdf</b> only."; return false; } lblError.innerHTML = ""; return true; } </script>

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 1:56 AM

All replies

  • User-271186128 posted

    Hi RJK,

    As far as I know, UploadedComplete event - Raised on the server when a file is uploaded successfully. So, I don't suggest you using this event to validate the file type.

    I suggest you could try to use AllowedFileTypes property. This property enables you to restrict the types of files that can be uploaded. You can assign a comma delimited list of file extensions to this property.

    For more details, please refer to this link: http://ajaxcontroltoolkit.devexpress.com/AjaxFileUpload/AjaxFileUpload.aspx

    Best Regards,
    Dillion

    Friday, August 7, 2015 2:39 AM
  • User-1746098369 posted

    Hi Zhi

    thanks for your reply, but the scenario is different , I have to restrict the mislenious contents to upload.

    user can change the extension of .aspx page to .pdf like 1.aspx.pdf and can be able to upload it 

    in such case AllowedFileTypes will not restrict it and it can be harmful 

    hope you got my point , any other solution to restrict it please  share 

    Friday, August 7, 2015 3:34 AM
  • User-271186128 posted

    Hi RJK,

    user can change the extension of .aspx page to .pdf like 1.aspx.pdf and can be able to upload it 

    As for this issue, I suggest you could check the filename, whether it contains another extension, such as (.aspx, .txt). I suggest you could try to use Asp.Net File upload control, then when click the submit button, you could get the filename and validate it using JavaScript or JQuery. Here is a sample, you could refer to it:

            <div>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:RequiredFieldValidator ErrorMessage="Required" ControlToValidate="FileUpload1"
                    runat="server" Display="Dynamic" ForeColor="Red" />
                <span id="lblError" style="color: red;"></span>
                <asp:Button ID="btnSubmit" runat="server" Text="Upload" OnClientClick="return MyFunction();" />
            </div>

    JavaScript code:

        <script type="text/javascript">
            function MyFunction() {
                var fileUpload = document.getElementById("FileUpload1");
                var lblError = document.getElementById("lblError");
    
                var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(.pdf)$");
                
                var denyFiles = [".doc", ".docx", ".aspx"];
                var regex2 = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + denyFiles.join('|') + ")+(.pdf)$");
    //Check whether it contains multiple extension. if (regex2.test(fileUpload.value.toLowerCase())) { lblError.innerHTML = "Please check the upload files, the extensions not right."; return false; } else if (!regex.test(fileUpload.value.toLowerCase())) { //Check whether it is a pdf file. lblError.innerHTML = "Please upload files having extensions: <b>pdf</b> only."; return false; } lblError.innerHTML = ""; return true; } </script>

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 1:56 AM