【问题标题】:Display modal while files are uploading文件上传时显示模式
【发布时间】:2015-02-18 19:04:59
【问题描述】:

我正在构建一个 c# MVC 应用程序,我想知道如何在上传文件时显示一个包含请稍候消息的模式。

我在 Goggle 和这个网站上搜索了很多,但我可以使用一些更直接的反馈。

我在我的 index.cshtml 中创建了模式,但不确定如何在上传过程中显示它。我尝试在我的提交按钮中使用类似的东西,但它没有发布表单。

 data-toggle="modal" data-target="#myModal"

这是我的代码。

索引.cshtml

<h4>Please fill out the form below and select at least one file to upload.</h4>

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="row">
        <div class="col-md-2">
            <h5>Your Name:</h5>
        </div>
        <div class="col-md-4">
            <input type="text" name="uname" class="form-control" required placeholder="John Smith">
        </div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <h5>Your Email:</h5>
        </div>
        <div class="col-md-4">
            <input type="email" name="email" class="form-control" required placeholder="test@test.com">
        </div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <h5>Your Company:</h5>
        </div>
        <div class="col-md-4">
            <input type="text" name="company" class="form-control" required placeholder="Test Company, Inc">
        </div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <h5>Choose file(s) to upload (Max 500MB):</h5>
        </div>
        <div class="col-md-4">
            <input name="files" type="file" id="files" multiple="multiple" class="form-control" required />
        </div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <h5></h5>
        </div>
        <div class="col-md-4">
            <input type="submit" name="submit" value="Upload" class="btn btn-primary" />
        </div>
    </div>
}

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                Please wait while we are uploading your files
                <div class="progress">
                    <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

那么这里是我的控制器

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;

namespace vidup.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Index(List<HttpPostedFileBase> files)
        {
            var userName = Request["uname"].ToString();
            var userEmail = Request["email"].ToString();
            var userCompany = Request["company"].ToString();
            ViewBag.Username = userName;
            ViewBag.UserCompany = userCompany;
            ViewBag.UserEmail = userEmail;
            int i = 0;

            var path = Path.Combine(Server.MapPath("~/Uploads"), userCompany, userName, DateTime.Now.ToString("MM-dd-yyyy h-mm-tt"));
            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }

            foreach (HttpPostedFileBase item in files)
            {
                i++;
                if (item != null && item.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(item.FileName);
                    var fullPath = Path.Combine(path, fileName);
                    ViewBag.Message3 = fileName;
                    ViewBag.Message4 = fullPath;
                    try
                    {
                        item.SaveAs(fullPath);
                        var fileCount = i + " File(s) uploaded successfully";
                        ViewBag.Message = fileCount;
                    }
                    catch (Exception e)
                    {
                        ViewBag.Message = e;
                    }
                }
                else
                {
                    ViewBag.Message = "No File selected";
                }
            }




            return View("Status");
        }
    }
}

最后是status.cshtml

<h2>Upload Status</h2>

<h4>Thank you, @ViewBag.UserName from @ViewBag.UserCompany</h4>
<P>Your file upload returned a status of: @ViewBag.Message</P>
<p>An Email has been sent to @ViewBag.UserEmail with the status of this upload.</p>
<br/>
<a href="@Url.Action("Index")" class="btn btn-primary">Click here to upload again</a>

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    您的提交按钮不再发布表单的原因是因为您放入其中的属性覆盖了其默认行为 - 除了模态显示之外,您还需要向其添加 onclick 处理程序以启动表单提交.给你的提交按钮一个 ID 并给你的表单一个 ID.. 之后它是一个简单的 onclick 方法

    $('buttonid').on('click', function()
    {
      $('#formid').submit();
    });
    

    这告诉您的按钮在单击时提交表单

    【讨论】:

    • 这就是我所拥有的,但它不起作用。 &lt;script&gt; document.getElementById('sbmtBtn').on('click', function () { $('#upldFrm').submit(); }); &lt;/script&gt;
    • 嗯,一定是缓存问题,因为它现在在 chrome 的隐身窗口上工作。
    • 非常乐意提供帮助:)
    • 在同一个注释中,有没有一种方法可以轻松地将 chrome 显示在右下角的百分比显示为进度条的百分比?请参阅此图片以供参考link
    • 默认情况下,您不能轻松地创建自己的进度条..,但我认为您永远无法知道上传文件所需的确切时间,所以我认为您会有时会在页面到达 100% 之前导航离开
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2020-08-19
    • 1970-01-01
    • 2021-08-23
    • 2023-04-07
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多