I've got the AjaxFileUpload
control working just fine -- it uploads, and on completion calls the server-side code to move the files around, works just fine, etc etc etc.
What I'm worried about are potential server-side errors, and how to hand back some sort of warning to the user. Not an Ajax error, but something on the server-side code.
Now, I've got log4net running just fine, and it's called in my error-trapping code and merrily dumping logs when I hard-code an error.
But that doesn't tell the users anything.
RegisterStartupScript
doesn't seem to be a valid solution, because there's no PostBack that would allow it to operate (same as my first attempt at populating fields. doh!).
Now, I can shove some JS into the ClientUploadComplete
or ClientUploadCompleteAll
to do a PostBack... but that doesn't seem right, and it would require that server-side error-messages be queued-up for display. Plus, it clears out the AjaxFileUpload
display of what has been uploaded.
All the error-handling I've seen regarding these controls is for Ajax errors. Is there anything for server-side issues that allows for easy feedback to the user?
Am I even barking up the right trees?
UPDATE Using the pointers @ Yuriy's other answer I've got the following working:
Onn the server side:
protected void OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
try
{
// does something with the uploaded files that _might_ fail
}
catch (Exception ex)
{
var ce = Logger.LogError(ex);
var msg = string.Format("{{ 'id': '{0}', 'message': '{1}'}}",
ce.ErrorId, ce.Message);
e.PostedUrl = msg;
}
}
(The Logger dumps the complete exception into a log4net log, and returns an error-number and consumer-friendly message to contact support)
Back on the page, the AjaxFileUpload
control is configured to call the JS when complete:
<asp:AjaxFileUpload runat="server" ID="Uploader" MaximumNumberOfFiles="10"
OnUploadComplete="OnUploadComplete"
OnClientUploadComplete="AjaxFileUpload1_OnClientUploadComplete"/>
And the javascript:
<script type="text/javascript">
function AjaxFileUpload1_OnClientUploadComplete(sender, args) {
var errText = args.get_postedUrl();
if (!errText) return; // only process if populated
var errinfo = Sys.Serialization.JavaScriptSerializer.deserialize(errText);
if (errinfo && errinfo.id && errinfo.message) {
var idEl = document.getElementById('errnbr');
var msgEl = document.getElementById('errmsg');
if (idEl && msgEl) {
idEl.innerHTML = errinfo.id;
msgEl.innerHTML = errinfo.message;
}
}
}
</script>
which populates the following:
<div class="failureNotification" id="ErrorDisplay" runat="server">
<span id="errnbr"><asp:Literal ID="ErrorNumber" runat="server"></asp:Literal></span>
<span id="errmsg"><asp:Literal ID="FailureText" runat="server"></asp:Literal></span>
</div>
Although
AjaxFileUploadState
enumeration includeFailed
member I can't find any case where it used.So there are two solutions available I believe.
The first is to tweak ACT project to add setters to
State
andStatusMessage
properties ofAjaxFileUploadEventArgs
class and handle values of these properties on client inraiseUploadComplete
function of Sys.Extended.UI.AjaxFileUpload.Control class andonUploadCompleteHandler
of Sys.Extended.UI.AjaxFileUpload.ProcessorHtml5 class.Or you can pass custom JSON to client via
AjaxFileUploadEventArgs.PostedUrl
property, deserialize it on client inOnClientUploadComplete
handler and show error message if any. Please check this question for sample of usagePostedUrl
property: getting asynfileupload controls file name on button click