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.
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,
Secondly modify your code,
Finally add updatepanel and register asynctrigger,
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.