<%@ Page Title="Resume Upload" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" %>
<%@ Register Assembly="AjaxUploader" Namespace="AjaxUploader.Controls" TagPrefix="au" %> 
<asp:Content ID="TitleContent" ContentPlaceHolderID="TitleContent" runat="server">Resume Upload - AjaxUploader v5.2 Demos</asp:Content>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderTitle" runat="server">Resume Upload</asp:Content> 
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 
<div class="demo-card"> <div class="demo-card-header">Resume Upload After Interruption</div> <div class="demo-card-body"> <p class="demo-description"> Chunked uploads can be resumed after a network interruption or browser close. The server
 tracks which chunks have been received so only the remaining chunks are uploaded on retry.
 </p> <au:AjaxFileUpload ID="Uploader1" runat="server"
 EnableChunkedUpload="true"
 EnableResume="true"
 ChunkSize="1048576"
 AutoUpload="true" /> <div class="mt-3"> <button type="button" class="btn btn-warning btn-sm" id="btnSimulate" disabled>Simulate Interruption</button> <button type="button" class="btn btn-success btn-sm" id="btnResume" disabled>Resume Upload</button> </div> <div id="log" class="mt-3 p-2 bg-light border rounded" role="log" aria-live="polite" style="max-height:180px;overflow-y:auto;font-size:.85rem;"></div> <div class="code-panel"> <pre><code>&lt;au:AjaxFileUpload ID="Uploader1" runat="server"
 EnableChunkedUpload="true"
 EnableResume="true"
 ChunkSize="1048576"
 AutoUpload="true" /&gt;

// JavaScript API approach
var uploader = AjaxUploader.create('#uploader', {
 uploadUrl: '<%= ResolveUrl("~/ajaxupload.axd/upload") %>',
 chunkUrl: '<%= ResolveUrl("~/ajaxupload.axd/chunk") %>',
 chunked: true,
 chunkSize: 1 * 1024 * 1024,
 autoUpload: true,
 onTaskError: function(task, error) {
 log('Interrupted: ' + task.fileName + ' - can resume from chunk ' + task.chunkIndex);
 }
});

// Resume: uploader.retryTask(taskId);</code></pre> </div> </div>
</div> 
</asp:Content> 
<asp:Content ID="ScriptsContent" ContentPlaceHolderID="ScriptsContent" runat="server">
<script> var logEl = document.getElementById('log');
 var btnSimulate = document.getElementById('btnSimulate');
 var btnResume = document.getElementById('btnResume');
 var currentTaskId = null;
 function clampPercent(value) {
 var pct = Number(value);
 if (!isFinite(pct)) return 0;
 return Math.max(0, Math.min(100, Math.round(pct)));
 }

 function log(msg) {
 if (!logEl) return;
 var div = document.createElement('div');
 div.textContent = '[' + new Date().toLocaleTimeString() + '] ' + msg;
 logEl.appendChild(div);
 logEl.scrollTop = logEl.scrollHeight;
 }

 var uploader = window.AjaxUploader && AjaxUploader.getInstance ? AjaxUploader.getInstance('Uploader1') : null;
 if (uploader) {
 uploader.on('taskStart', function(task) {
 currentTaskId = task.id;
 if (btnSimulate) {
 btnSimulate.disabled = false;
 }
 log('Started: ' + task.fileName);
 });
 uploader.on('taskProgress', function(task) {
 log('Progress: ' + task.fileName + ' ' + clampPercent(task.percent) + '%');
 });
 uploader.on('taskError', function(task, error) {
 log('Interrupted: ' + task.fileName);
 if (btnResume) {
 btnResume.disabled = false;
 }
 if (btnSimulate) {
 btnSimulate.disabled = true;
 }
 });
 uploader.on('taskComplete', function(task) {
 log('Completed: ' + task.fileName);
 if (btnSimulate) {
 btnSimulate.disabled = true;
 }
 if (btnResume) {
 btnResume.disabled = true;
 }
 });
 }

 if (btnSimulate) {
 btnSimulate.addEventListener('click', function() {
 if (currentTaskId && uploader && uploader.pauseTask) {
 uploader.pauseTask(currentTaskId);
 log('Upload paused - simulating network interruption.');
 if (btnResume) {
 btnResume.disabled = false;
 }
 btnSimulate.disabled = true;
 }
 });
 }

 if (btnResume) {
 btnResume.addEventListener('click', function() {
 if (currentTaskId && uploader && uploader.retryTask) {
 uploader.retryTask(currentTaskId);
 log('Resuming upload...');
 btnResume.disabled = true;
 if (btnSimulate) {
 btnSimulate.disabled = false;
 }
 }
 });
 }
</script>
</asp:Content>
