AjaxFileUpload:如何提醒用户OnUploadComplete中的服务器端错误? [英] AjaxFileUpload: How can I alert the user to a server-side error in OnUploadComplete?

查看:242
本文介绍了AjaxFileUpload:如何提醒用户OnUploadComplete中的服务器端错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 AjaxFileUpload 控制工作正常 - 它上传,完成后,调用服务器端代码移动文件,工作正常,等等。



我担心的是潜在的服务器端错误,以及如何将某种警告提交给用户。不是一个Ajax错误,而是服务器端的代码。



现在,我已经有log4net运行正常,它被我的错误捕获代码和当我硬编码错误时,快速转储日志。



但这并不告诉用户任何东西。



RegisterStartupScript 似乎不是一个有效的解决方案,因为没有PostBack可以让它运行(就像我第一次尝试填充字段doh!)。 / p>

现在,我可以将一些JS推送到 ClientUploadComplete ClientUploadCompleteAll 做一个PostBack ...但是似乎不正确,它需要服务器端的错误消息排队等待显示。此外,它清除了 AjaxFileUpload 显示已上传的内容。



所有的错误处理看到这些控件是针对Ajax错误的。
有没有什么可以让服务器端的问题容易地向用户反馈?



我甚至咆哮着正确的树?

$使用指针@ Yuriy的其他答案我有以下工作:



在服务器端:

  protected void OnUploadComplete(object sender,AjaxFileUploadEventArgs e)
{
try
{
//与上传的文件做某事_might_ fail
}
catch(Exception ex)
{
var ce = Logger.LogError前);

var msg = string.Format({{'id':'{0}','message':'{1}'}},
ce.ErrorId,ce 。信息);
e.PostedUrl = msg;
}
}

(Logger将完整的异常转储到log4net日志中,并返回错误号码和消费者友好的消息以联系支持)



返回页面, AjaxFileUpload 控件配置为在完成时调用JS:

 < asp:AjaxFileUpload runat =serverID =Uploader MaximumNumberOfFiles =10
OnUploadComplete =OnUploadComplete
OnClientUploadComplete =AjaxFileUpload1_OnClientUploadComplete/>

而javascript:

 < script type =text / javascript> 
函数AjaxFileUpload1_OnClientUploadComplete(sender,args){

var errText = args.get_postedUrl();
if(!errText)return; //仅当进程填充

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>

填充以下内容:

 < div class =failureNotificationid =ErrorDisplayrunat =server> 
< span id =errnbr>< asp:Literal ID =ErrorNumberrunat =server>< / asp:Literal>< / span>
< span id =errmsg>< asp:Literal ID =FailureTextrunat =server>< / asp:Literal>< / span>
< / div>


解决方案

尽管 AjaxFileUploadState 枚举包括失败成员我找不到任何使用的情况。



所以我相信有两个可用的解决方案。



第一个是调整ACT项目以将设置添加到状态 StatusMessage 属性 AjaxFileUploadEventArgs 中提供客户端上的这些属性的类和值处理raiseUploadComplete Sys.Extended.UI.AjaxFileUpload.C​​ontrol class和 onUploadCompleteHandler Sys.Extended.UI.AjaxFileUpload.ProcessorHtml5 类。



或者您可以通过 AjaxFileUploadEventArgs.PostedUrl 属性将自定义JSON传递给客户端,在客户端以 OnClientUploadComplete 处理程序并显示错误消息(如果有)。请检查此问题的使用示例 PostedUrl 属性:获取asynfileupload控件文件名按钮点击


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 include Failed 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 and StatusMessage properties of AjaxFileUploadEventArgs class and handle values of these properties on client in raiseUploadComplete function of Sys.Extended.UI.AjaxFileUpload.Control class and onUploadCompleteHandler of Sys.Extended.UI.AjaxFileUpload.ProcessorHtml5 class.

Or you can pass custom JSON to client via AjaxFileUploadEventArgs.PostedUrl property, deserialize it on client in OnClientUploadComplete handler and show error message if any. Please check this question for sample of usage PostedUrl property: getting asynfileupload controls file name on button click

这篇关于AjaxFileUpload:如何提醒用户OnUploadComplete中的服务器端错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆