【发布时间】:2011-04-29 10:03:54
【问题描述】:
我有一个 MVC3 应用程序,它从用户的硬盘上传文件并对其进行操作。一项要求是文件扩展名应为 .xls(或 xlsx)。
我想验证文件名,但我想知道在可重用性和性能方面什么是最佳选择(当然还有最佳编码实践)。
我有以下索引视图:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<br />
<p><input type="file" id="file" name="file" size="23"/></p><br />
<p><input type="submit" value="Upload file" /></p>
}
由控制器动作方法调用Index:
public ActionResult Index()
{
return View("Index");
}
并且用户响应由 Index 操作方法(HttpPost)处理:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
FileServices lfileServices = new FileServices();
var lfilePath = lfileServices.UploadFile(file, "~/App_Data/uploads");
//Manipulation;
}
现在我可以使用 FileServices 中的Path.GetExtension(filename) 来获取扩展名,然后执行检查,但它将在上传完成后执行。
我想在用户尝试上传文件后执行此操作。我唯一想到的就是创建一个模型(或者更好的是 ViewModel)并使用 DataAnnotations 中的远程验证。 p>
我在现场看到了 Scott Hanselman 的演示,但他似乎对此并不满意,因为应用程序正在编译但没有执行检查。
有没有人有一个好的方法来执行这种远程验证或任何其他解决方案(例如 jQuery)?
谢谢
弗朗西斯科
【问题讨论】:
标签: c# file asp.net-mvc-3