Search Results for

# Legacy UploadAttachments

UploadAttachments is the compatibility control for form-style attachment lists.

Use it when an older page expects:

- an attachment table UI
- the `Items` collection
- `AttachmentItem` event hooks
- optional custom attachment providers

## Typical markup

    <%@ Register Assembly="AjaxUploader" Namespace="CuteWebUI" TagPrefix="CuteWebUI" %>

    <CuteWebUI:UploadAttachments ID="Attachments1" runat="server"
        InsertText="Upload multiple files"
        MultipleFilesUpload="true"
        ShowQueueTable="true"
        ShowTableHeader="true"
        ShowRemoveButtons="true"
        OnFileUploaded="Attachments1_FileUploaded"
        OnAttachmentAdded="Attachments1_AttachmentAdded"
        OnAttachmentRemoveClicked="Attachments1_AttachmentRemoveClicked">
        <ValidateOption AllowedFileExtensions="pdf,doc,docx,xls,xlsx,zip" MaxSizeKB="20480" />
    </CuteWebUI:UploadAttachments>

## Typical code-behind

    protected void Attachments1_FileUploaded(object sender, CuteWebUI.UploaderEventArgs e)
    {
        litLog.Text += "<div>Uploaded: " + Server.HtmlEncode(e.FileName) + "</div>";
    }

    protected void Attachments1_AttachmentAdded(object sender, CuteWebUI.AttachmentItemEventArgs e)
    {
        e.Item.SetCustomData("category", "invoice");
    }

    protected void Attachments1_AttachmentRemoveClicked(object sender, CuteWebUI.AttachmentItemEventArgs e)
    {
        e.Behavior = CuteWebUI.AttachmentItemBehavior.AutoRemove;
    }

## Working with the collection

    foreach (CuteWebUI.AttachmentItem item in Attachments1.Items)
    {
        if (item.ShouldSave)
        {
            var path = Server.MapPath("~/App_Data/Attachments/" + item.FileName);
            using (var input = item.OpenStream())
            using (var output = System.IO.File.Create(path))
            {
                input.CopyTo(output);
            }
        }
    }

## Automatic postback (4.x parity)

As in 4.x, UploadAttachments posts back automatically when the upload batch finishes. `AttachmentAdded`, `AttachmentCreated`, and `FileUploaded` fire on the server with no submit button, the item enters the `Items` collection, and the attachment row renders - all with no extra code. This is on by default; set `AutoPostBack="false"` if a normal submit button already drives the form and you would rather process the attachments then.

## Custom provider scenarios

`CustomProvider` is for pages that mix AjaxUploader temp files with records that already exist somewhere else.

The provider contract lives at IAttachmentProvider.

## Related types

- AttachmentItem
- AttachmentItemCollection
- AttachmentItemEventArgs