<%@ Page Title="Server Events" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" %>
<%@ Register Assembly="AjaxUploader" Namespace="AjaxUploader.Controls" TagPrefix="au" %> 
<script runat="server"> protected void Uploader1_FileUploaded(object sender, AjaxUploader.Events.FileUploadedEventArgs e)
 {
 var uploadService = new AjaxUploader.Services.UploadService();
 var targetFolder = Server.MapPath("~/App_Data/Processed");
 System.IO.Directory.CreateDirectory(targetFolder);

 var safeFileName = System.IO.Path.GetFileName(e.FileName ?? string.Empty);
 if (safeFileName.Length > 0)
 {
 uploadService.CopyFile(e.FileGuid, System.IO.Path.Combine(targetFolder, safeFileName));
 }

 litLog.Text += "<div>" + Server.HtmlEncode(safeFileName) + " (" + e.FileSize + " bytes)</div>";
 }

 protected void BtnProcess_Click(object sender, EventArgs e)
 {
 if (Uploader1.UploadedFileGuids.Count == 0)
 {
 lblStatus.CssClass = "alert alert-warning mt-3";
 lblStatus.Text = "Upload a file first, then click Process Uploaded Files to trigger the server event on postback.";
 }
 else
 {
 lblStatus.CssClass = "alert alert-success mt-3";
 lblStatus.Text = "Processed " + Uploader1.UploadedFileGuids.Count + " uploaded file(s). Copies were written to App_Data/Processed.";
 }

 lblStatus.Style["display"] = "block";
 }
</script> 
<asp:Content ID="TitleContent" ContentPlaceHolderID="TitleContent" runat="server">Server Events - AjaxUploader v5.2 Demos</asp:Content>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderTitle" runat="server">Server Events</asp:Content> 
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 
<div class="demo-card"> <div class="demo-card-header">Server-Side FileUploaded Event</div> <div class="demo-card-body"> <p class="demo-description"> Handle the server-side <code>FileUploaded</code> event on the next Web Forms postback.
 Upload files first, then submit the page to process the uploaded GUIDs on the server.
 </p> <div class="alert alert-info py-2 px-3 small mb-3"><strong>Two things that trip people up:</strong> <code>FileUploaded</code> fires on the <em>postback</em>, not during the transfer &mdash; without a submit, your handler never runs and the temp copy is swept after 60 minutes. If your page has no submit flow of its own, set <code>AutoPostBack="true"</code> on the control and the page posts back automatically when the batch finishes, exactly like v4 did. And the destination you pass to <code>CopyFile</code>/<code>MoveTo</code> accepts <code>~/</code> app-relative paths (resolved against the site root) or absolute paths; a bare relative path is also anchored at the site root, never the worker process directory.</div> <au:AjaxFileUpload ID="Uploader1" runat="server"
 OnFileUploaded="Uploader1_FileUploaded"
 ShowProgress="true" /> <asp:Button ID="BtnProcess" runat="server" Text="Process Uploaded Files"
 CssClass="btn btn-primary btn-sm mt-3" OnClick="BtnProcess_Click" /> <asp:Label ID="lblStatus" runat="server" Style="display:none;" /> <div class="mt-3"> <div class="fw-semibold mb-2">Processed Files</div> <asp:Literal ID="litLog" runat="server" /> </div> <div class="code-panel"> <pre><code>&lt;%-- ASPX Markup --%&gt;
&lt;au:AjaxFileUpload ID="Uploader1" runat="server"
 OnFileUploaded="Uploader1_FileUploaded"
 ShowProgress="true" /&gt;

&lt;%-- No submit flow on the page? Post back automatically when the
     batch finishes (v4 behaviour) instead of adding a button: --%&gt;
&lt;au:AjaxFileUpload ID="Uploader2" runat="server"
 AutoPostBack="true"
 OnFileUploaded="Uploader1_FileUploaded" /&gt;

&lt;asp:Button runat="server" Text="Process Uploaded Files"
 OnClick="BtnProcess_Click" /&gt;

// Code-behind or &lt;script runat="server"&gt;
protected void Uploader1_FileUploaded(object sender, FileUploadedEventArgs e)
{
 var uploadService = new UploadService();
 var targetFolder = Server.MapPath("~/App_Data/Processed");
 var safeFileName = Path.GetFileName(e.FileName ?? string.Empty);
 Directory.CreateDirectory(targetFolder);
 uploadService.CopyFile(
 e.FileGuid,
 Path.Combine(targetFolder, safeFileName));
}</code></pre> </div> <h5 class="mt-4">Try It</h5> <p class="text-muted small"> Upload one or more files, then click <strong>Process Uploaded Files</strong> to raise
 <code>FileUploaded</code> during the page postback.
 </p> </div>
</div> 
</asp:Content> 
<asp:Content ID="ScriptsContent" ContentPlaceHolderID="ScriptsContent" runat="server">
<script> var uploader = window.AjaxUploader && AjaxUploader.getInstance ? AjaxUploader.getInstance('Uploader1') : null;
 function clampPercent(value) {
 var pct = Number(value);
 if (!isFinite(pct)) return 0;
 return Math.max(0, Math.min(100, Math.round(pct)));
 }
 if (uploader) {
 uploader.on('taskProgress', function(task) {
 var status = document.getElementById('<%= lblStatus.ClientID %>');
 if (!status) return;
 status.className = 'text-muted small d-block mt-2';
 status.textContent = 'Uploaded to temp storage: ' + task.fileName + ' ' + clampPercent(task.percent) + '%';
 status.style.display = '';
 });
 uploader.on('taskComplete', function(task) {
 var status = document.getElementById('<%= lblStatus.ClientID %>');
 if (!status) return;
 status.className = 'text-muted small d-block mt-2';
 status.textContent = 'Ready for server processing on postback: ' + task.fileName;
 status.style.display = '';
 });
 }
</script>
</asp:Content>
