AjaxFileUpload:我怎样才能提醒用户在OnUploadComplete服务器端错误? [英] AjaxFileUpload: How can I alert the user to a server-side error in OnUploadComplete?

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

问题描述

我已经得到了 AjaxFileUpload 控制工作得很好 - 它上传,并在​​完成调用服务器端code左右移动的文件,作品就好了,等等等等。

什么我担心的是潜力的服务器端错误,以及如何交回某种警示用户。没有一个Ajax错误的事,但在服务器端code。

现在,我已经得到log4net的运行蛮好的,这就是所谓的在我的错误捕获code和欢快转储日志的时候我硬code错误。

但是,这并没有告诉用户什么。

的RegisterStartupScript 似乎并没有一个有效的解决方案,因为没有回传,将允许其经营(同我在填充领域的首次尝试。卫生署!)

现在,我可以推一些JS到 ClientUploadComplete ClientUploadCompleteAll 做回发...但是,这看起来不正确,这将需要服务器端错误的消息进行排队,为的显示。另外,它清除出来的东西已经上传了 AjaxFileUpload 显示。

所有的错误处理,我对这些控件看到的是对Ajax的错误。
是否有服务器端的问题,任何可以很容易地反馈给用户?

我是连吠叫右树?


更新使用指针@ <一个href=\"http://stackoverflow.com/questions/18743107/getting-asynfileupload-controls-file-name-on-button-click/18761934#18761934\">Yuriy's 我有以下工作的其他答案:

翁服务器端:

 保护无效OnUploadComplete(对象发件人,AjaxFileUploadEventArgs E)
    {
        尝试
        {
            //确实与_might_失败上传的文件
        }
        赶上(异常前)
        {
            VAR CE = Logger.LogError(除息);            VAR味精=的String.Format({{ID:{0},信息:{1}'}},
                 ce.ErrorId,ce.Message);
            e.PostedUrl =味精;
        }
    }

(记录仪转储完整的例外成log4net的日志,并返回一个错误号码和用户友好的信息与技术支持联系)

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

 &LT; ASP:AjaxFileUpload =服务器ID =上传MaximumNumberOfFiles =10
OnUploadComplete =OnUploadComplete
OnClientUploadComplete =AjaxFileUpload1_OnClientUploadComplete/&GT;

和JavaScript:

 &LT;脚本类型=文/ JavaScript的&GT;
    功能AjaxFileUpload1_OnClientUploadComplete(发件人,参数){        变种errText = args.get_postedUrl();
        如果(errText!)回报; //唯一的过程,如果人口        VAR errinfo = Sys.Serialization.JavaScriptSerializer.deserialize(errText);        如果(errinfo&安培;&安培; errinfo.id和放大器;&安培; errinfo.message){
            变种IDEL =的document.getElementById('errnbr');
            VAR msgEl =的document.getElementById('ERRMSG');
            如果(IDEL&安培;&安培; msgEl){
                idEl.innerHTML = errinfo.id;
                msgEl.innerHTML = errinfo.message;
            }
        }
    }
&LT; / SCRIPT&GT;

其中填充以下内容:

 &LT; D​​IV CLASS =failureNotificationID =ErrorDisplay=服务器&GT;
    &LT;跨度ID =errnbr&GT;&LT; ASP:文字ID =ErrorNumber=服务器&GT;&LT; / ASP:文字&GT;&LT; / SPAN&GT;
    &LT;跨度ID =ERRMSG&GT;&LT; ASP:文字ID =FailureText=服务器&GT;&LT; / ASP:文字&GT;&LT; / SPAN&GT;
&LT; / DIV&GT;


解决方案

虽然 AjaxFileUploadState 枚举包括失败成员,我无法找到它使用的任何情况。

因此​​,有两种解决方案可我相信。

首先是要调整ACT项目制定者添加到国家 StatusMessage 属性 AjaxFileUploadEventArgs 类,并在 raiseUploadComplete 的<函数href=\"https://ajaxcontroltoolkit.$c$cplex.com/SourceControl/latest#Server/AjaxControlToolkit/AjaxFileUpload/AjaxFileUpload.Control.$p$p.js\"相对=nofollow> Sys.Extended.UI.AjaxFileUpload.C​​ontrol 类和 onUploadCompleteHandler Sys.Extended.UI.AjaxFileUpload.ProcessorHtml5 类。

或者你也可以通过 AjaxFileUploadEventArgs.PostedUrl 属性传递自定义JSON客户端,在 OnClientUploadComplete 反序列化的客户端处理程序如果任何显示错误消息。请检查这个问题的用法示例 PostedUrl 属性:<一个href=\"http://stackoverflow.com/questions/18743107/getting-asynfileupload-controls-file-name-on-button-click/18761934#18761934\">getting 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天全站免登陆