3

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.

4

3 回答 3

4

昨天我们遇到了同样的问题,我们发现在同一页面上不能有多个AjaxFileUpload 实例。

如果查看源代码,您会发现该控件使用常量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)
于 2012-05-31T08:37:25.937 回答
2

我们对 2012 年 9 月的工具包进行了如下定制 - 希望这是一个临时解决方法,并在未来的版本中修复:

老的

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

新的

private string ContextKey = "";

老的

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

新的

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();
        }
于 2012-12-11T17:18:20.527 回答
1

实际上有一种方法可以在单个页面上使用多个 AjaxFileUpload 控件,每个控件触发自己的事件。解决方法很简单;它涉及为 AjaxFileUpload 控件覆盖 Microsoft 的客户端函数之一,以在控件上注入实际导致上传完成事件的信息,然后将所有 AjaxFileUpload 控件的单个事件处理程序用作“交换机”,随后将触发创建事件服务器端的控件的正确事件处理程序。

这是如何做到的:

在页面的 head 元素之后的某处添加此脚本块。如果您使用母版页,请将其放在 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>

此代码与用于调用您的页面并触发 UploadComplete 事件的函数相同,只是修改为添加了一个额外的参数 - uplCtrlID - 它将包含真正导致事件的控件的 ID。

如下设置您的服务器端代码:

//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
    }

你都准备好了。同一页面上有多个 AjaxFileUpload 控件,没有问题。

于 2013-06-07T15:05:48.613 回答