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

rakeshkuma
 
on Mar 25, 2021 01:12 AM
3026 Views

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

ASPX

<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>

ASPX.VB

    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 !!"
        Else
            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
                Session.Remove(Session.Keys(i))
                Exit For
            End If
        Next
    End Sub

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
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.

HTML

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<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>

Code

C#

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);
    }
    else
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", 
            "top.$get('" + Label39.ClientID + "').innerHTML ='Upload only .pdf file !!';", true);
    }
}

VB.Net

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)
    Else
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "error", _
                                                "top.$get('" & Label39.ClientID & "').innerHTML ='Upload only .pdf file !!';", True)
    End If
End Sub