asp.net - How to determine whether FileUpload control has a file

FileUpload HasFile Property
FileUpload is an asp.net web server control. FileUplaod control allow users to upload a file from their computer to web server.

FileUpload control's HasFile property get a value indicating whether the FileUpload control contains a file. HasFile property value data type is System.Boolean. this property return 'true', if the FileUpload control contains a file; otherwise HasFile property return 'false'.

FileUpload control's HasFile property allow us to determine whether user select a file to upload. if an user does not select a file to upload and press the upload button then we can verify it by using FileUpload control's HasFile property.

we can send an error message to the specified user to select a file to upload. if the FileUpload control exists a file to upload then we can perform other task such as save it to server by SaveAs method.

the following asp.net c# example code demonstrate us how can we check whether FileUpload control contains a file programmatically at run time in an asp.net application.
FileUploadHasFile.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string uploadFolder = Request.PhysicalApplicationPath + "Upload\\";
            FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
            Label1.ForeColor = System.Drawing.Color.Green;
            Label1.Text = "File uploaded successfully: " + FileUpload1.PostedFile.FileName;
        }
        else
        {
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "Please select a file first!";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net FileUpload example: how to check whether FileUpload control has file</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Maroon">asp.net FileUpload example: FileUpload HasFile</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Larger"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             ForeColor="DarkGreen"
             Text="Choose a file for upload it."
             Font-Bold="true"
             >
        </asp:Label>
        <br />
        <asp:FileUpload 
             ID="FileUpload1" 
             runat="server" 
             BackColor="DarkGreen" 
             ForeColor="AliceBlue" 
             />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             Font-Bold="true" 
             ForeColor="DarkGreen" 
             OnClick="Button1_Click"
             Text="Upload Now"
             />   
        <br /><br />
    </div>
    </form>
</body>
</html>
More asp.net examples