I've been following this tutorial , but I got a problem.

If an exception is thrown in the UploadedComplete event

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);

            }
        }

I want the exception message shown in the label that is below the AsyncFileUpload control.

I thought that using a try catch and passing the message using ScriptManager.RegisterClientScriptBlock would be enough and that message will be passed to this client side function

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

But even if a exception is thrown, the controls ignores it and show a successful message.

I also noticed that the status parameter in this function never gets updated. It is always "success"

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");
            }
        }

Do you have any idea what can be causing this???

Thanks in advance.

2

There are 2 answers

2
Sawyer On

I think the prerequisite to use these two client side fonctions is whether a file is successfully uploaded or not but not the after examination. So if you want to send the Exception message to user, like the format of excel. It will be better to do some workarouds. Because I don't have enough time to realize this, some advices and ideas will be listed below for you if I am correct.

Firstly a function js should be added in the page,

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");
            }
}

Secondly modify your code,

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);
                }
            }

Finally add updatepanel and register asynctrigger,

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

The code pseudo means that the event UploadCompletd will update LabelMessage in UpdatePanel and trigger the function validfile.

I am not sure if these code could be executed directly.

I adviced you to use firebug to test if the funciton js could be called after file uploaded.

0
Rananga On

This is an old question but thought of posting this in case some body come here searching for this

Since AsyncFileUpload do it's magic in an iframe you should use ScriptManager.RegisterStartupScript instead of ScriptManager.RegisterClientScriptBlock