MVC4回拨从服务器到客户端 [英] MVC4 Call back from server to client

查看:120
本文介绍了MVC4回拨从服务器到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC应用4,我需要在客户端显示的消息,由控制器发送消息给客户。

我需要将文件上传到服务器,并做一些处理在foreach循环中,一旦各的foreach我需要在用户界面中显示的信息。
目前,我有我循环需要在这种情况下,从服务器发送信息到客户端上的每个循环

查看

  @using(Html.BeginForm(指数,家,FormMethod.Post,新{ID =formUpload,ENCTYPE =的multipart / form-data的}) )
{
    < D​​IV>
        < B>上传文件< / B>
        <输入类型=文件名称=文件/>
        <输入类型=提交值=上传文件NAME =btnUpload的onclick =progressStatus();/>< BR />
    < / DIV>
    < D​​IV>
        @ ViewBag.Message
    < / DIV>
    < D​​IV的风格=宽度:30%;保证金:0汽车;>
        < D​​IV ID =进度的风格=宽度:300像素,高度:15px的>< / DIV>
        < BR />
    < / DIV>
}

控制器code

  [HttpPost]
公众的ActionResult指数(HttpPostedFileBase文件)
{
    如果(文件!= NULL)
    {
        VAR FNAME = Path.GetFileName(file.FileName);
        VAR EXIS = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(〜/存储/上传),FNAME);
        如果(System.IO.File.Exists(EXIS))
        {
            计算机[消息] =文件+ FNAME +已经存在;
        }
        其他
        {
            尝试
            {
                如果(file.ContentLength大于0)
                {
                    变种文件名= Path.GetFileName(file.FileName);
                    VAR FOLDERPATH =使用Server.Mappath(〜/存储/上传);
                    FNAME =文件名;
                    VAR路径= Path.Combine(FOLDERPATH,文件名);
                    VAR filebytes =新的字节[file.ContentLength]
                    如果(!Directory.Exists(FOLDERPATH))
                        Directory.CreateDirectory(FOLDERPATH);
                    file.SaveAs(路径);
                    的for(int i = 0; I< 20;我++)
                    {
                        //显示此消息在UI从这里每次运行像我想显示用户消息1,2,3,4每次运行等
                    }
                }
                计算机[消息] =文件+ FNAME +上传successully
            }
            赶上(例外五)
            {
                计算机[消息] =文件+ FNAME +无法上传;
                计算机[消息] = e.Message;
            }
        }
    }
    其他
        计算机[消息] =请选择文件;
                返回查看();
}


解决方案

 您可以用`StringBuilder`:    VAR SB =新的StringBuilder();    的for(int i = 0; I< 20;我++)
    {
         BOOL的isValid = DoSomething的();         如果(的isValid)
         {
            sb.Append(<立GT;循环的成功:+ I +< /李>);
         }
         其他
         {
            sb.Append(<李>在环路错误:+ I +/李>&LT);
          }      }    viewBag.msg = sb.toString();

I am using ASP.NET MVC 4 application, I need to Display messages in the Client, by sending messages from Controller to Client.

I need to Upload a file to Server and Do Some Processing in the Foreach loop and Once each foreach i need to display message in the UI. Currently i have for Loop I need to Send message from Server to Client on each for loop in this case

View

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "formUpload", enctype = "multipart/form-data" }))
{
    <div>
        <b>Upload File</b>
        <input type="file" name="file" />
        <input type="submit" value="Upload File" name="btnUpload" onclick="progressStatus();"/><br />
    </div>
    <div>
        @ViewBag.Message
    </div>
    <div style="width: 30%; margin: 0 auto;">
        <div id="progressbar" style="width: 300px; height: 15px"></div>
        <br />
    </div>
}

Controller Code

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    if (file != null)
    {
        var fname = Path.GetFileName(file.FileName);
        var exis = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Storage/uploads"), fname);
        if (System.IO.File.Exists(exis))
        {
            ViewData["Message"] = "The file " + fname + " has already exists";
        }
        else
        {
            try
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var folderPath = Server.MapPath("~/Storage/uploads");
                    fname = fileName;
                    var path = Path.Combine(folderPath, fileName);
                    var filebytes = new byte[file.ContentLength];
                    if (!Directory.Exists(folderPath))
                        Directory.CreateDirectory(folderPath);
                    file.SaveAs(path);
                    for (int i = 0; i < 20; i++)
                    {
                        //Display This Message in UI From here each time for runs like i want to show user message 1,2,3,4 etc each time for runs
                    }
                }
                ViewData["Message"] = "The file " + fname + " has uploaded successully";
            }
            catch (Exception e)
            {
                ViewData["Message"] = "The file " + fname + " Could not upload";
                ViewData["Message"] = e.Message;
            }
        }
    }
    else
        ViewData["Message"] = "Please choose file";
                return View();
}

解决方案

You can use `StringBuilder`:

    var sb = new StringBuilder();      

    for (int i = 0; i < 20; i++)
    {
         bool isValid = doSomeThing();

         if (isValid)
         {
            sb.Append("<li>Loop success : " + i + "</li>");     
         }
         else
         {
            sb.Append("<li>Error in loop : " + i + "</li>");         
          }

      }

    viewBag.msg = sb.toString();

这篇关于MVC4回拨从服务器到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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