文件上传时显示模态

时间:2015-02-18 19:05:00

标签: c# asp.net-mvc

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

我经常搜索Goggle和这个网站,但我可以使用更直接的反馈。

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

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

这是我的代码。

Index.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>

1 个答案:

答案 0 :(得分:0)

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

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

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