Ajax toolkit file cannot be called

I have two ajaxtoolkit ulopads files on the same page as

 <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 for

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 that when I use the AjaxFileUpload1 download, it works and calls the void ajaxUpload2_OnUploadComplete method, but if I use ajaxUpload1, the ajaxUpload2_OnUploadComplete method is called again, but the ajaxUpload1 method is not called

Why??

Thank.

+3
source share
3 answers

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

, , GUID, . GUID , AjaxFileUpload GUID...

:

...

GUID :

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

(...)

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

2012 : , :

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

NEW

private string ContextKey = "";

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();
        }
+2

AjaxFileUpload , . ; Microsoft AjaxFileUpload , , AjaxFileUpload "", , .

:

script - . -, HTML:

<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 that is used to call your page and fires the UploadComplete event, only to add an additional parameter - uplCtrlID - which will contain the identifier of the control that REALLY triggered the event.

Configure the 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 are all set. Multiple AjaxFileUpload controls on one page, no problem.

+1
source

All Articles