asp.net - How to handle error during uploading a file

File upload error handling
FileUploadErrorHandle.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        try
        {
            string uploadFolder = Request.PhysicalApplicationPath + "Upload\\";
            FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
            Label1.ForeColor = System.Drawing.Color.Green;
            Label1.Text = "File uploaded successfully: " + FileUpload1.PostedFile.FileName;
        }
        catch(Exception ex)
        {
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "an error occured.<br />";
            Label1.Text += ex.Message;
        }
    }
</script>

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