<%@ Page Title="Server-Side Validation" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" %>
<%@ Register Assembly="AjaxUploader" Namespace="AjaxUploader.Controls" TagPrefix="au" %>

<script runat="server">
    private readonly System.Collections.Generic.List<string> _acceptedFiles = new System.Collections.Generic.List<string>();
    private readonly System.Collections.Generic.List<string> _rejectedFiles = new System.Collections.Generic.List<string>();

    protected void Uploader1_FileValidating(object sender, AjaxUploader.Events.FileValidatingEventArgs e)
    {
        var fileName = e.FileName ?? "(unnamed)";

        if (e.FileSize <= 0)
        {
            e.Cancel = true;
            e.CancelReason = "Empty files are not allowed.";
        }
        else if (fileName.IndexOf("..", StringComparison.Ordinal) >= 0)
        {
            e.Cancel = true;
            e.CancelReason = "Relative path segments are not allowed.";
        }
        else if (string.Equals(System.IO.Path.GetExtension(fileName), ".tmp", StringComparison.OrdinalIgnoreCase))
        {
            e.Cancel = true;
            e.CancelReason = ".tmp files are blocked by server policy.";
        }

        if (e.Cancel)
        {
            _rejectedFiles.Add(fileName + " - " + (e.CancelReason ?? "Rejected by server policy."));
        }
        else
        {
            _acceptedFiles.Add(fileName);
        }
    }

    protected void BtnValidate_Click(object sender, EventArgs e)
    {
        if (_acceptedFiles.Count == 0 && _rejectedFiles.Count == 0)
        {
            lblStatus.CssClass = "alert alert-warning mt-3";
            lblStatus.Text = "Upload one or more files first, then click Run Server Validation.";
        }
        else
        {
            lblStatus.CssClass = "alert alert-info mt-3";
            lblStatus.Text = "Validated " + (_acceptedFiles.Count + _rejectedFiles.Count) + " file(s): "
                + _acceptedFiles.Count + " accepted, " + _rejectedFiles.Count + " rejected.";
        }

        lblStatus.Visible = true;
        litAccepted.Text = RenderItems(_acceptedFiles);
        litRejected.Text = RenderItems(_rejectedFiles);
    }

    private string RenderItems(System.Collections.Generic.IEnumerable<string> items)
    {
        var sb = new System.Text.StringBuilder();
        foreach (var item in items)
        {
            sb.Append("<div>");
            sb.Append(Server.HtmlEncode(item));
            sb.Append("</div>");
        }

        return sb.ToString();
    }
</script>

<asp:Content ID="TitleContent" ContentPlaceHolderID="TitleContent" runat="server">Server-Side Validation - AjaxUploader v5.0 Demos</asp:Content>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderTitle" runat="server">Server-Side Validation</asp:Content>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<div class="demo-card">
    <div class="demo-card-header">Server-Side Validation</div>
    <div class="demo-card-body">
        <p class="demo-description">
            Handle server-side validation using the <code>FileValidating</code> event on postback.
            This demo accepts normal files and rejects empty uploads, path traversal names, and
            <code>.tmp</code> files when you submit the page.
        </p>

        <au:AjaxFileUpload ID="Uploader1" runat="server"
            AutoUpload="true"
            ButtonText="Upload File"
            ShowProgress="true"
            OnFileValidating="Uploader1_FileValidating" />

        <asp:Button ID="BtnValidate" runat="server" Text="Run Server Validation"
            CssClass="btn btn-primary btn-sm mt-3" OnClick="BtnValidate_Click" />
        <asp:Label ID="lblStatus" runat="server" Visible="false" />
        <div class="row mt-3">
            <div class="col-md-6">
                <div class="fw-semibold mb-2">Accepted</div>
                <asp:Literal ID="litAccepted" runat="server" />
            </div>
            <div class="col-md-6">
                <div class="fw-semibold mb-2">Rejected</div>
                <asp:Literal ID="litRejected" runat="server" />
            </div>
        </div>

        <div class="code-panel">
            <pre><code>&lt;!-- .aspx markup --&gt;
&lt;au:AjaxFileUpload ID="Uploader1" runat="server"
    AutoUpload="true"
    ButtonText="Upload File"
    ShowProgress="true"
    OnFileValidating="Uploader1_FileValidating" /&gt;

&lt;!-- Code-behind (.aspx.cs) --&gt;
protected void Uploader1_FileValidating(object sender, FileValidatingEventArgs e)
{
    if (e.FileSize == 0)
    {
        e.Cancel = true;
        e.CancelReason = "Empty file.";
        return;
    }
    if (e.FileName.Contains(".."))
    {
        e.Cancel = true;
        e.CancelReason = "Invalid filename.";
        return;
    }
}</code></pre>
        </div>
    </div>
</div>

</asp:Content>
