Note that there are some explanatory texts on larger screens.

plurals
  1. POAjaxFileUpload: How can I alert the user to a server-side error in OnUploadComplete?
    primarykey
    data
    text
    <p>I've got the <code>AjaxFileUpload</code> 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.</p> <p>What I'm worried about are <em>potential</em> 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.</p> <p>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.</p> <p>But that doesn't tell the users anything.</p> <p><code>RegisterStartupScript</code> 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!).</p> <p>Now, I can shove some JS into the <code>ClientUploadComplete</code> or <code>ClientUploadCompleteAll</code> 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 <code>AjaxFileUpload</code> display of what has been uploaded.</p> <p>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?</p> <p>Am I even barking up the right trees?</p> <hr> <p><strong>UPDATE</strong> Using the pointers @ <a href="https://stackoverflow.com/questions/18743107/getting-asynfileupload-controls-file-name-on-button-click/18761934#18761934">Yuriy's other answer</a> I've got the following working:</p> <p>Onn the server side:</p> <pre><code> 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; } } </code></pre> <p>(The Logger dumps the complete exception into a log4net log, and returns an error-number and consumer-friendly message to contact support)</p> <p>Back on the page, the <code>AjaxFileUpload</code> control is configured to call the JS when complete:</p> <pre><code>&lt;asp:AjaxFileUpload runat="server" ID="Uploader" MaximumNumberOfFiles="10" OnUploadComplete="OnUploadComplete" OnClientUploadComplete="AjaxFileUpload1_OnClientUploadComplete"/&gt; </code></pre> <p>And the javascript:</p> <pre><code>&lt;script type="text/javascript"&gt; 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 &amp;&amp; errinfo.id &amp;&amp; errinfo.message) { var idEl = document.getElementById('errnbr'); var msgEl = document.getElementById('errmsg'); if (idEl &amp;&amp; msgEl) { idEl.innerHTML = errinfo.id; msgEl.innerHTML = errinfo.message; } } } &lt;/script&gt; </code></pre> <p>which populates the following:</p> <pre><code>&lt;div class="failureNotification" id="ErrorDisplay" runat="server"&gt; &lt;span id="errnbr"&gt;&lt;asp:Literal ID="ErrorNumber" runat="server"&gt;&lt;/asp:Literal&gt;&lt;/span&gt; &lt;span id="errmsg"&gt;&lt;asp:Literal ID="FailureText" runat="server"&gt;&lt;/asp:Literal&gt;&lt;/span&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload