【发布时间】:2013-05-09 19:10:04
【问题描述】:
我是 MVC 编码的初学者。
当应用程序启动时,ViewBag.Message 是:选择要上传的文件。
上传成功后变为:文件上传成功!
有没有办法让它返回并在大约 5 秒后再次显示“选择要上传的文件”消息,而不使用任何 javascript? 我想如果 mvc 有一些我可以使用的内置时间函数?
https://github.com/xoxotw/mvc_fileUploader
我的看法:
@{
ViewBag.Title = "FileUpload";
}
<h2>FileUpload</h2>
<h3>Upload a File:</h3>
@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
@Html.ValidationSummary();
<input type="file" name="fileToUpload" /><br />
<input type="submit" name="Submit" value="upload" />
@ViewBag.Message
}
我的控制器:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Mvc_fileUploader.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Choose a file to upload!";
return View("FileUpload");
}
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase fileToUpload)
{
if (ModelState.IsValid)
{
if (fileToUpload != null && fileToUpload.ContentLength > (1024 * 1024 * 1)) // 1MB limit
{
ModelState.AddModelError("fileToUpload", "Your file is to large. Maximum size allowed is 1MB !");
}
else
{
string fileName = Path.GetFileName(fileToUpload.FileName);
string directory = Server.MapPath("~/fileUploads/");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string path = Path.Combine(directory, fileName);
fileToUpload.SaveAs(path);
ModelState.Clear();
ViewBag.Message = "File uploaded successfully!";
}
}
return View("FileUpload");
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
【问题讨论】:
-
您必须为此使用 javascript。为什么要寻找非 javascript 解决方案?
标签: c# asp.net-mvc-4 viewbag