<%@ 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.0 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> <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;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();
 uploadService.CopyFile(
 e.FileGuid,
 Server.MapPath("~/App_Data/Processed/" + e.FileName));
}</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 = AjaxUploader.getInstance('Uploader1');
 if (uploader) {
 uploader.on('taskProgress', function(task) {
 var status = document.getElementById('<%= lblStatus.ClientID %>');
 status.className = 'text-muted small d-block mt-2';
 status.textContent = 'Uploaded to temp storage: ' + task.fileName + ' ' + Math.round(task.percent) + '%';
 status.style.display = '';
 });
 uploader.on('taskComplete', function(task) {
 var status = document.getElementById('<%= lblStatus.ClientID %>');
 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>
