C# 上载文件时显示模式
我正在构建一个c MVC应用程序,我想知道在上传文件时如何显示一个包含请等待消息的模式 我已经搜索了很多Goggle和这个网站,但我可以使用一些更直接的反馈 我已经在index.cshtml中创建了模式,但不确定在上传过程中如何显示它。我试着在我的提交按钮中使用类似的东西,但它没有发布表单C# 上载文件时显示模式,c#,asp.net-mvc,C#,Asp.net Mvc,我正在构建一个c MVC应用程序,我想知道在上传文件时如何显示一个包含请等待消息的模式 我已经搜索了很多Goggle和这个网站,但我可以使用一些更直接的反馈 我已经在index.cshtml中创建了模式,但不确定在上传过程中如何显示它。我试着在我的提交按钮中使用类似的东西,但它没有发布表单 data-toggle="modal" data-target="#myModal" 这是我的密码 Index.cshtml <h4>Please fill out the form belo
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>
submit按钮不再发布表单的原因是,您在表单中添加的属性将覆盖其默认行为-除了模式显示之外,您还需要向其添加onclick处理程序以启动表单提交。给提交按钮一个ID,给表单一个ID。。之后,这是一个简单的onclick方法
$('buttonid').on('click', function()
{
$('#formid').submit();
});
这会告诉您的按钮在每次单击时提交表单这是我得到的,但它不起作用。getElementById'sbmtBtn'。单击,函数{$'upldFrm.submit;};好吧,一定是缓存问题导致它现在在一个不知名的chrome窗口上工作。很高兴能提供帮助:同样,有没有一种方法可以轻松地将chrome在右下角显示的百分比显示为其上传进度条的百分比?查看此图像以供参考并非默认情况下,您可以轻松创建自己的进度条,但我认为您永远无法知道上载文件所需的确切时间,因此我认为有时页面在到达100%之前导航时,您会得到