Ajax toolkit file upload is not called

2019-04-12 01:36发布

问题:

I have two ajaxtoolkit file ulopads on the same page like

 <ajaxToolkit:AjaxFileUpload
        id="AjaxFileUpload1"
        AllowedFileTypes="jpg,jpeg,gif,png"
        OnUploadComplete="ajaxUpload2_OnUploadComplete"
        runat="server"  />
         <ajaxToolkit:AjaxFileUpload
        id="ajaxUpload1"
        AllowedFileTypes="jpg,jpeg,gif,png"
        OnUploadComplete="ajaxUpload1_OnUploadComplete"
        runat="server"  />

and code behind

protected void ajaxUpload2_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {

        string filePath = "~/Images/" + e.FileName;
        filePath = filePath.Split('\\').Last();
        Session["img2"] = filePath.ToString();
        AjaxFileUpload1.SaveAs(MapPath(filePath));

    }

    protected void ajaxUpload1_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {

        string filePath = "~/Images/" + e.FileName;
        filePath = filePath.Split('\\').Last();
        Session["img1"] = filePath.ToString();
        ajaxUpload1.SaveAs(MapPath(filePath));

    }

The question is whenever I use upload AjaxFileUpload1 it works on and calls void ajaxUpload2_OnUploadComplete method but if I use ajaxUpload1 the method ajaxUpload2_OnUploadComplete is called again but the method ajaxUpload1 is not called

Why??

Thanks.

回答1:

We got the same problem yesterday and we found out that you cannot have more than one instance of AjaxFileUpload on the same page.

If you look at the source code, you'll see that this control use a constant GUID to identify its events. Since the GUID is a constant, all instances of AjaxFileUpload use the same GUID...

Result :

the first instance swallow all the events...

Here is the GUID in action :

private const string ContextKey = "{DA8BEDC8-B952-4d5d-8CC2-59FE922E2923}";

(...)

if (this.Page.Request.QueryString["contextkey"] == ContextKey && this.Page.Request.Files.Count > 0)


回答2:

We customized the September 2012 toolkit as follows - hope this is a temporary workaround and that this is fixed in a future release:

OLD

private const string ContextKey = "{DA8BEDC8-B952-4d5d-8CC2-59FE922E2923}";

NEW

private string ContextKey = "";

OLD

public AjaxFileUpload()
            : base(true, HtmlTextWriterTag.Div)
        {
        }

NEW

public AjaxFileUpload()
            : base(true, HtmlTextWriterTag.Div)
        {
            if (HttpContext.Current.Items["lastAjaxFileUploadContextKey"] == null)
            {
                HttpContext.Current.Items["lastAjaxFileUploadContextKey"] = 1;
            }
            else
            {
                HttpContext.Current.Items["lastAjaxFileUploadContextKey"] = (int)HttpContext.Current.Items["lastAjaxFileUploadContextKey"] + 1;
            }

            ContextKey = HttpContext.Current.Items["lastAjaxFileUploadContextKey"].ToString();
        }


回答3:

There actually is a way to use multiple AjaxFileUpload controls on a single page, with each control firing its own event. The solution is very simple; it involves overriding one of Microsoft's client-side functions for the AjaxFileUpload control to inject information on the control that actually caused the upload complete event, then using a single event handler for all of the AjaxFileUpload controls as a "switchboard", which will subsequently fire the correct event handler for the control which created the event server-side.

Here's how to do it:

Add this script block somewhere after the head element of your page. If you're using master pages, put this in a placeholder for HTML content:

<script type="text/javascript">
    Sys.Extended.UI.AjaxFileUpload.Control.prototype.doneAndUploadNextFile = function (c) {
        var a = new XMLHttpRequest, b = this;
        a.open("POST", "?contextKey=" + this._contextKey + "&done=1&guid=" + c._id + "&uplCtrlID=" + b.get_id(), true);
        a.onreadystatechange = function () {
            if (a.readyState == 4) if (a.status == 200) {
                b.raiseUploadComplete(Sys.Serialization.JavaScriptSerializer.deserialize(a.responseText));
                b._processor.startUpload()
            }
            else {
                b.setFileStatus(c, "error", Sys.Extended.UI.Resources.AjaxFileUpload_error);
                b.raiseUploadError(a);
                throw "error raising upload complete event and start new upload";
            }
        };
        a.send(null);
    }
</script>

This code is the same function being used to call your page and trigger the UploadComplete event, only modified to add an extra parameter - uplCtrlID - which will contain the ID of the control that REALLY caused the event.

Set up your server side code as follows:

//set the OnUploadComplete property on all of your AjaxFileUpload controls to this method
protected void anyUploader_UploadComplete(object sender, AjaxFileUploadEventArgs e)
    {
        //call the correct upload complete handler if possible
        if (Request.QueryString["uplCtrlID"] != null)
        {
            //uplCtrlID (the query string param we injected with the overriden JS function)
            //contains the ID of the uploader.
            //We'll use that to fire the appropriate event handler...
            if (Request.QueryString["uplCtrlID"] == FileUploaderA.ClientID)
                FileUploaderA_UploadComplete(FileUploaderA, e);
            else if (Request.QueryString["uplCtrlID"] == FileUploaderB.ClientID)
                FileUploaderB_UploadComplete(FileUploaderB, e);
            //etc (or use a switch block - whatever suits you)
        }
    }
    protected void FileUploaderA_UploadComplete(AjaxFileUpload sender, AjaxFileUploadEventArgs e)
    {
        //logic here
    }
    protected void FileUploaderB_UploadComplete(AjaxFileUpload sender, AjaxFileUploadEventArgs e)
    {
        //logic here
    }

You're all set. Multiple AjaxFileUpload controls on the same page, no problems.