File extension validation in AsyncFileUpload Control on server side (code behind) in ASP.Net

on Mar 25, 2021 01:12 AM

As per below code I am trying to upload only .pdf file and displaying error but Label38 and Label39 is not showing as per user action.

how we can do it from server side.

Upload only pdf file in AsyncFileUpload in ASP.NET


<ajax:AsyncFileUpload ID="AsyncFileUpload1" CssClass="form-control" OnClientUploadError="uploadError" 
    CompleteBackColor="White" Width="100%" runat="server" UploaderStyle="Modern" UploadingBackColor="White" 
    ThrobberID="imgLoad" OnUploadedComplete="fileUploadComplete" />
<asp:Image ID="imgLoad" runat="server" ImageUrl="loading.gif" />
<asp:Label ID="Label38" runat="server"  Font-Size="12px" ForeColor="Green" ></asp:Label>
<asp:Label ID="Label39" runat="server" Font-Size="12px" ForeColor="Red" Text="Error"></asp:Label>


    Protected Sub fileUploadComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs)     
        Dim filename As String = System.IO.Path.GetFileName(AsyncFileUpload1.FileName)
        Dim filenamex As String = System.IO.Path.GetExtension(AsyncFileUpload1.FileName)
        If filenamex.ToString() = ".pdf" Then
            AsyncFileUpload1.SaveAs(Server.MapPath("documents/") + filename)
            ClearContents(CType(sender, Control))
            Label38.Text = "Upload done !!"
            Label39.Visible = True
            Label39.Text = "Upload only .pdf file !!"
        End If
    End Sub
    Private Sub ClearContents(ByVal Control As Control)
        For i As Integer = 0 To Session.Keys.Count - 1
            If Session.Keys(i).Contains(Control.ClientID) Then
                Exit For
            End If
    End Sub


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Mar 26, 2021 06:03 AM

Hi rakeshkuma,

If you want to display any information using OnUploadedComplete from server side, its not possible.

One work around is that you use ScriptManager RegisterClientScriptBlock().

Refer below code.


<asp:ScriptManager ID="ScriptManager1" runat="server">
<ajax:AsyncFileUpload ID="AsyncFileUpload1" CssClass="form-control" CompleteBackColor="White"
    Width="100%" runat="server" UploaderStyle="Modern" UploadingBackColor="White" ThrobberID="imgLoad"
    OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
<asp:Image ID="imgLoad" runat="server" ImageUrl="~/images/loader.gif" />
<asp:Label ID="Label38" runat="server" Font-Size="12px" ForeColor="Green"></asp:Label>
<asp:Label ID="Label39" runat="server" Font-Size="12px" ForeColor="Red"></asp:Label>



protected void AsyncFileUpload1_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
    string extension = Path.GetExtension(e.filename);
    if (extension.ToLower() == ".pdf")
        AsyncFileUpload1.SaveAs(Server.MapPath("documents/") + Path.GetFileName(e.filename));
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", 
            "top.$get(\"" + Label38.ClientID + "\").innerHTML ='Upload done !!';", true);
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", 
            "top.$get('" + Label39.ClientID + "').innerHTML ='Upload only .pdf file !!';", true);


Protected Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As AsyncFileUploadEventArgs)
    Dim extension As String = Path.GetExtension(e.filename)
    If extension.ToLower() = ".pdf" Then
        AsyncFileUpload1.SaveAs(Server.MapPath("documents/") + Path.GetFileName(e.filename))
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "success", _
                                                "top.$get(""" & Label38.ClientID & """).innerHTML ='Upload done !!';", True)
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "error", _
                                                "top.$get('" & Label39.ClientID & "').innerHTML ='Upload only .pdf file !!';", True)
    End If
End Sub