简体   繁体   English

MVC4从服务器回调客户端

[英]MVC4 Call back from server to client

I am using ASP.NET MVC 4 application, I need to Display messages in the Client, by sending messages from Controller to Client. 我正在使用ASP.NET MVC 4应用程序,我需要通过从Controller向客户端发送消息来在客户端中显示消息。

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. 我需要将文件上传到Server并在Foreach循环中执行一些处理,并且每次foreach我都需要在UI中显示消息。 Currently i have for Loop I need to Send message from Server to Client on each for loop in this case 目前我对循环我需要在这种情况下在每个for循环上从服务器发送消息到客户端

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 create dictionary key value pair in each iteration of loop like 您可以在循环的每次迭代中创建字典键值对

for (int i = 0; i < 20; i++)
                {
                    ViewData["Message_"+i.ToString()] = //your message;
                }
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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM