Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET

Asynchronous File Upload

Rate me:
Please Sign up or sign in to vote.
4.96/5 (26 votes)
13 Jun 2014CPOL12 min read 126.9K   5K   103  
An Ajax control that enables a user to upload a file asynchronously with extra data
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GalleryExample.aspx.cs" Inherits="GalleryExample"
    Async="true" MasterPageFile="~/Master/MasterPage.master" Title="Gallery Example" %>


<asp:Content ID="headContect" runat="server" ContentPlaceHolderID="head">
    <link href="../css/employees.css" rel="stylesheet" type="text/css" />
    <script src="../script/utility.js" type="text/javascript"></script>
    <script src="../script/GellaryExample.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="bodyContect" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <div style="padding: 20px; width: 600px">
        <div style="vertical-align: middle;">
            <div class="textportion">
                <span style="font-weight: bold; font-size: large;">Gallaries</span>
            </div>
            <div class="buttonportion">
                <asp:LinkButton ID="cmdNewGallery" runat="server" Text="New Gallery" OnClick="cmdNewGallery_Click"
                    CausesValidation="false">
                </asp:LinkButton>
            </div>
            <div class="cleardiv">
            </div>
        </div>
        <div>
            <asp:UpdatePanel ID="pnlGrid" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:GridView ID="grdGalleris" runat="server" AutoGenerateColumns="False" GridLines="None"
                        OnRowCommand="grdGalleris_RowCommand">
                        <Columns>
                            <asp:TemplateField HeaderStyle-Width="16px">
                                <ItemTemplate>
                                    <asp:LinkButton ID="cmdEdit" runat="server" CommandName="EditGallery" CommandArgument='<%# Eval("GalleryID") %>'
                                        CausesValidation="false">
                                    <%# Eval("GalleryName") %>
                                    </asp:LinkButton>
                                </ItemTemplate>
                                <HeaderStyle Width="16px" />
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </div>
    <a id="lnkTarget" runat="server"></a>
    <ajax:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="lnkTarget"
        BackgroundCssClass="modalbackground" PopupControlID="GalleryeDialog">
    </ajax:ModalPopupExtender>
    <div id="GalleryeDialog" runat="server" class="dialog">
        <div class="titlebar">
            <div class="textportion">
                <span class="titletext">Gallery Dialog</span>
            </div>
            <div class="buttonportion">
                <a href="javascript:;" onclick="javascript:$find('<%= ModalPopupExtender1.ClientID %>').hide();"
                    class="closebutton">X</a>
            </div>
            <div class="cleardiv">
            </div>
        </div>
        <asp:UpdatePanel ID="pnlDialog" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <input type="hidden" id="hgId" runat="server" />
                <div style="padding: 5px">
                    <asp:LinkButton ID="cmdReset" runat="server" Text="New" />&nbsp;
                    <asp:LinkButton ID="cmdSubmit" runat="server" Text="Save & New" />&nbsp; <a href="javascript:;"
                        onclick="javascript:$find('<%= AjaxUploadGalleryExtender1.ClientID %>').submit(function(){$find('<%= ModalPopupExtender1.ClientID %>').hide()});">
                        Save & Close</a>
                </div>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
                <asp:RequiredFieldValidator ID="reqGalleryName" runat="server" ControlToValidate="txtGalleryName"
                    Display="None" ErrorMessage="Please enter the Gallery Name."></asp:RequiredFieldValidator>
                <div style="padding: 2px 0px">
                    <label for="<%= txtGalleryName.ClientID %>" style="display: inline-block; width: 100px;">
                        Gallery Name <span style="color: Red;">*</span>:</label>
                    <asp:TextBox ID="txtGalleryName" runat="server" Width="275px"></asp:TextBox>
                </div>
                <div style="padding: 5px">
                    <div class="textportion">
                        &nbsp;
                    </div>
                    <div class="buttonportion">
                        <asp:LinkButton ID="cmdAddImage" runat="server">Add Image</asp:LinkButton>
                    </div>
                    <div class="cleardiv">
                    </div>
                </div>
                <div id="blkPicturs" runat="server" style="width: 544px; height: 471px;">
                    <asp:Repeater ID="grdPictures" runat="server" OnItemDataBound="grdPictures_ItemDataBound">
                        <ItemTemplate>
                            <div style="float: left; border: solid 1px blue; margin: 2px">
                                <div style="background-color: blue;">
                                    <div class="textportion">
                                        <asp:Label ID="lblTitle" runat="server" Style="color: White;" Text='<%# Eval("Title") %>'>
                                        </asp:Label>
                                        <asp:HiddenField ID="hdnpid" runat="server" Value='<%# Eval("pid") %>' />
                                        <asp:HiddenField ID="hdnTitle" runat="server" Value='<%# Eval("title") %>' />
                                    </div>
                                    <div class="buttonportion">
                                        <a id="cmdRemove" runat="server" href="javascript:;" style="background-color: Red;
                                            color: White; border: solid 1px blue;">X</a>
                                    </div>
                                    <div class="cleardiv">
                                    </div>
                                </div>
                                <div style="padding: 0px 5px;">
                                    <iframe id="PreviewFrame" runat="server" style="width: 240px; height: 180px; border: solid 1px gray;"
                                        src='<%# ResolveUrl("~/GalleryImages/") + Eval("filename") %>' frameborder="0" scrolling="no"
                                        marginheight="0" marginwidth="0"></iframe>
                                </div>
                                <div style="background-color: gray">
                                    <div class="textportion">
                                        <a id="cmdBrowse" runat="server" href="javascript:;">Browse</a>
                                    </div>
                                    <div class="buttonportion">
                                        <a id="cmdClear" runat="server" href="javascript:;">Clear</a>
                                    </div>
                                    <div class="cleardiv">
                                    </div>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </div>
                <web:AjaxUploadGalleryExtender ID="AjaxUploadGalleryExtender1" runat="server" AutoPreview="true"
                    ContextKey="GIMG" EnableProgress="OnSubmit, OnPreview" EvaluateFunctionName="GellaryExample.EvaluateFunction"
                    Filter="*.jpg;*.bmp;*.gif;*.png|Supported Images Types (*.jpg;*.bmp;*.gif;*.png)"
                    MaxNoOfFiles="4" OnAddingFileFunction="GellaryExample.AddingFileFunction" OnMaxNumberExssededFunction="GellaryExample.OnMaxNumberExssededFunction"
                    OnProgressFeedbackFunction="GellaryExample.OnProgressChanged" OnRemovingFileFunction="GellaryExample.RemovingFileFunction"
                    OnResetFunction="GellaryExample.OnResetFuncction" OnSubmitCompleteFunction="GellaryExample.OnCompleteFunction"
                    OnSubmitFunction="GellaryExample.OnSubmitFunction" OnUnsupportedFunction="GellaryExample.OnUnsupportedFunction"
                    PostBackUrl="~/CrossPostBack/GallerySavePage.aspx" ProgressServiceMethod="GetProgressValue"
                    ProgressServicePath="~/CrossPostBack/GallerySavePage.aspx" ResetButtonID="cmdReset" OnPreviewCompleteFunction="GellaryExample.OnPreviewCompleteFunction"
                    SubmitButtonID="cmdSubmit" TargetControlID="cmdAddImage" ZIndex="10002" OnPreviewFunction="GellaryExample.OnPreviewFunction">
                    <ExtraData>
                        <web:DataEntry Name="gId" EvaluateKey="gId" />
                        <web:DataEntry Name="name" EvaluateKey="name" />
                        <web:DataEntry Name="except" EvaluateKey="except" />
                    </ExtraData>
                </web:AjaxUploadGalleryExtender>
                <!--
                -->
            </ContentTemplate>
        </asp:UpdatePanel>
        <div id="message" style="color: Green">
        </div>
        <div id="loading" style="display: none">
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <td>
                        <img alt="Loading..." height="20" src="../Images/Loading.gif" width="20" />
                    </td>
                    <td>
                        <div>
                            <span id="lblProgress">Please wait... </span>
                        </div>
                        <div id="divProgress" style="padding: 3px; display: none;">
                            <div class="textportion" style="width: 500px; border: solid 1px silver;">
                                <div id="divBar" style="background-color: Lime; height: 10px;">
                                </div>
                            </div>
                            <div class="buttonportion">
                                <span id="lblPercent"></span>
                            </div>
                            <div class="cleardiv">
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <table>
        <tr>
            <td>
            </td>
            <td valign="top">
                <div style="padding: 2px 0px">
                    <asp:CheckBox ID="chkCancel" runat="server" Text="Cancel Submission." />
                </div>
                <div style="padding: 2px 0px">
                    <asp:CheckBox ID="chkException" runat="server" Checked="True" onclick="javascript:GellaryExample.ToggleOptionsEnabled(this.checked);"
                        Text="Throw an exception." />
                </div>
                <div style="padding: 2px 10px">
                    <asp:RadioButtonList ID="optException" runat="server">
                        <asp:ListItem Value="data" Selected="True">During saving data.</asp:ListItem>
                        <asp:ListItem Value="image">During saving image.</asp:ListItem>
                    </asp:RadioButtonList>
                </div>
            </td>
        </tr>
    </table>
</asp:Content>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) The first Ones
Jordan Jordan
-- Life is Good.
and you can make it better

Comments and Discussions