<%@ Page Language="c#" %>
<%@ Register Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" TagPrefix="CuteWebUI" %>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head runat="server">
    <title>Multiple Files Upload - v4 compatibility</title>
    <link rel="stylesheet" href="demo.css" type="text/css" />

    <script runat="server">
        void InsertMsg(string msg)
        {
            ListBoxEvents.Items.Insert(0, msg);
            ListBoxEvents.SelectedIndex = 0;
        }

        void Attachments1_AttachmentAdded(object sender, AttachmentItemEventArgs args)
        {
            InsertMsg(args.Item.FileName + " has been uploaded.");
        }

        // v4 raised FileValidating for every file before accepting it, and
        // UploadCompleted once for the batch. Both are restored in 5.3.2 - before
        // that they did not exist on UploadAttachments at all.
        void Attachments1_FileValidating(object sender, UploaderEventArgs args)
        {
            InsertMsg("Validating " + args.FileName + "...");
        }

        void Attachments1_UploadCompleted(object sender, UploaderEventArgs[] args)
        {
            InsertMsg("Batch complete: " + args.Length + " file(s).");
        }

        void ButtonDeleteAll_Click(object sender, EventArgs e)
        {
            InsertMsg("Attachments1.DeleteAllAttachments();");
            Attachments1.DeleteAllAttachments();
        }

        void ButtonTellme_Click(object sender, EventArgs e)
        {
            ListBoxEvents.Items.Clear();
            foreach (AttachmentItem item in Attachments1.Items)
            {
                InsertMsg(item.FileName + ", " + item.FileSize + " bytes.");

                //Copies the uploaded file to a new location.
                //item.CopyTo("c:\\temp\\"+item.FileName);
                //You can also open the uploaded file's data stream.
                //System.IO.Stream data = item.OpenStream();
            }
        }
    </script>
</head>
<body>
    <form id="Form1" method="post" runat="server">
        <div class="content">
            <a class="backlink" href="Default.aspx">&#8592; v4 compatibility demos</a>
            <h2>Multiple Files Upload</h2>
            <p>The attachments control keeps an uploaded-file list across postbacks. Enumerate
                <code>Items</code> on the server to copy or stream each file.</p>

            <div class="note">
                This page exercises the members restored in 5.3.2. <code>UploadAttachments</code>
                was rebased onto the modern control in v5 and lost 65 v4 members, including the
                <code>FileValidating</code> and <code>UploadCompleted</code> events wired up here -
                which previously could not fire at all.
            </div>

            <div class="panel">
                <CuteWebUI:UploadAttachments runat="server" ID="Attachments1"
                    InsertText="Upload Multiple files Now"
                    MultipleFilesUpload="true"
                    MaxFilesLimit="5"
                    MaxFilesLimitMsg="You may upload at most {0} files."
                    OnAttachmentAdded="Attachments1_AttachmentAdded"
                    OnFileValidating="Attachments1_FileValidating"
                    OnUploadCompleted="Attachments1_UploadCompleted" />
            </div>

            <div class="panel">
                <p>Server Trace:</p>
                <asp:ListBox runat="server" ID="ListBoxEvents" />
                <p>
                    <asp:Button ID="ButtonTellme" Text="List uploaded files" runat="server"
                        OnClick="ButtonTellme_Click" />
                    <asp:Button ID="ButtonDeleteAll" Text="Delete all" runat="server"
                        OnClick="ButtonDeleteAll_Click" />
                </p>
            </div>

            <div class="note">
                <strong>MaxFilesLimit</strong> is set to 5 above. Until 5.3.2 this property stored
                to ViewState and was never read, so the limit was silently ignored and any number
                of files was accepted.
            </div>
        </div>
    </form>
</body>
</html>
