Call AjaxFileUpload OnUploadComplete event on another Button click event using C# and jQuery in ASP.Net

Last Reply 6 days ago By Andrea

Posted 6 days ago

i have this ajax file upload contorl it works fine

what i want is instead of upload file on ajax control blue button i want to upload file on seperate button like 

i have this button on my page

 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

so in its click event i want to fire ajax file upload event, please adivce

<style>
    #MainContent_projectfiles_AjaxFileUpload11_FileStatusContainer
    {
        font-size: 0px;
    }
        
        
    #ajax__fileupload_topFileStatus
    {
        font-size: 0px;
    }
        
    #MainContent_projectfiles_AjaxFileUpload11_SelectFileContainer
    {
        font-size: 0px;
    }
</style>
<%@ register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload11" runat="server" MaximumNumberOfFiles="1"
    Width="400px" OnUploadComplete="OnUploadComplete" OnClientUploadCompleteAll="uploadcompleteAll"
    OnClientUploadError="uploadError" OnClientUploadStart="uploadstart" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Image ID="imgDisplay" runat="server" CssClass="image-s" />
<asp:Label ID="lblerror" runat="server" Text="" ForeColor="red"></asp:Label>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:Button ID="btnUpload" Text="Upload" runat="server" CausesValidation="false"
    OnClick="Upload" Style="display: none" />
<script type="text/javascript">
    function uploadComplete(sender) {
        var imgDisplay = document.getElementById("<%=imgDisplay.ClientID %>");
        imgDisplay.src = "images/loader.gif";
        imgDisplay.style.cssText = "";
        var img = new Image();
        img.onload = function () {
            imgDisplay.style.cssText = "height:100px;width:100px";
            imgDisplay.src = img.src;
        };
        img.src = "<%=ResolveUrl(UploadFolderPath) %>" + args.get_fileName();
    }
    function uploadError(sender) {
        $get("<%=Label1.ClientID%>").innerHTML = sender.innerHTML();
    }

    function uploadcompleteAll(sender) {
        alert("successfully uploaded All Files!");
        window.location.reload();
    }
    function uploadstart() {
        alert("File upload started, please wait while we are processing.");
    } 
</script>

 

protected void OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
    string myId = IDd;
    string acfilename = Path.GetFileName(e.FileName);
    string fileName = Path.GetFileName(e.FileName);
    string ext = Path.GetExtension(e.FileName);
    fileName = Guid.NewGuid().ToString() + ext;
    Session["f"] = fileName;
    AjaxFileUpload11.SaveAs(Server.MapPath("~/DynamicImages/" + fileName));
    UploadFolderPath = "~/DynamicImages/" + fileName;
}