ASP.net AsyncFileUpload:如何从'UploadedComplete'发送例外消息事件到客户端功能' uploadError'

时间:2013-12-04 11:08:57

标签: asp.net ajaxcontroltoolkit asyncfileupload

我一直关注这个tutorial,但我遇到了问题。

如果在UploadedComplete事件中抛出异常

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            try
            {
                string importPath = MapPath("~/docs/imports/");
                string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetFileName(e.FileName);

                //pass filename to front end;
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "newfile"
                    , "window.parent.$find('" + AsyncFileUpload1.ClientID + "').newFileName='" + filename + "';", true);

                AsyncFileUpload1.SaveAs(importPath + filename);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", "top.$get(\"" + lblUploadStatus.ClientID + "\").innerHTML = 'There was an Error Processing the Request : Error " + ex.Message.ToString() + "';", true);

            }
        }

我希望标签中显示的异常消息位于AsyncFileUpload控件之下。

我认为使用try catch并使用ScriptManager.RegisterClientScriptBlock传递消息就足够了,该消息将传递给此客户端函数

function uploadError(sender, args) {
            var errmsg = args.get_errorMessage();
            updateUploadStatus("error", "There was Error Uploading the file. Error :" + errmsg);
        }

但即使抛出异常,控件也会忽略它并显示成功的消息。

我还注意到此函数中的status参数永远不会更新。它总是“成功”

function updateUploadStatus(status, message) {
            var uploadstatlbl = $("span[id$='lblUploadStatus']");
            uploadstatlbl.html(message);
            if (status == "error") {
                uploadstatlbl.removeClass("spansuccess").addClass("spanerror");
            } else {
                uploadstatlbl.removeClass("spanerror").addClass("spansuccess");
            }
        }

你知道是什么导致了这个???

提前致谢。

2 个答案:

答案 0 :(得分:1)

这是一个老问题,但考虑过发布这个问题,以防某些机构来到这里寻找此

由于AsyncFileUploadiframe中发挥作用,你应该使用ScriptManager.RegisterStartupScript代替ScriptManager.RegisterClientScriptBlock

答案 1 :(得分:0)

我认为使用这两个客户端功能的先决条件是文件是否成功上传,而不是后检查。因此,如果要将Exception消息发送给用户,就像excel的格式一样。做一些workarouds会更好。因为我没有足够的时间来实现这一点,如果我是正确的,下面会列出一些建议和想法。

首先应在页面中添加一个函数js,

function validfile(status, message){
            var uploadstatlbl = $("span[id$='LabelMessage']");
            uploadstatlbl.html(message);
            if (status == "notvalid") {
                uploadstatlbl.removeClass("spansuccess").addClass("spanerror");
            } else {
                uploadstatlbl.removeClass("spanerror").addClass("spansuccess");
            }
}

其次修改您的代码,

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
            {
                try
                {
                    string importPath = MapPath("~/docs/imports/");
                    string filename = DateTime.Now.ToString("yyyyMMddhhmmss") +                      Path.GetFileName(e.FileName);

                    //pass filename to front end;
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "newfile"
                        , "window.parent.$find('" + AsyncFileUpload1.ClientID + "').newFileName='" + filename + "';", true);

                    AsyncFileUpload1.SaveAs(importPath + filename);
                    string script = "validfile('valide', 'The file is uploaded successfully');";
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", script, true);}
                catch (Exception ex)
                {
                    // I didn't test it in visual studio, it means we will call the method js after postback
                    string script = "validfile('notvalide', '" + ex.Message + "');";
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", script, true);
                }
            }

最后添加updatepanel并注册asynctrigger,

<asp:UpdatePanel ID="" runat="" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="AsyncFileUpload1" EventName="UploadedCompleted" />
    </Triggers>
    <ContentTemplate>
        <asp:Label ID="LabelMessage" />
    </ContentTemplate>
</asp:UpdatePanel>

代码伪意味着事件UploadCompletd将更新UpdatePanel中的LabelMessage并触发函数validfile。

我不确定这些代码是否可以直接执行。

我建议你使用firebug测试文件上传后是否可以调用函数js